Arduino : How to put quotation marks in a string ?

In Arduino programming many times you will come with situations where you want to put double quotes in a string. For example sending AT command with double quotes. There many different methods let’s discuss one by one.

How do I put quotes in a string?

AT+CPMS=”SM”

Serial.println(“AT+CPMS=”SM””);  // This results in error

Method 1: Escape sequences

Escape sequences are used to represent certain special characters within string literals and character literals.

The following escape sequences are available (extra escape sequences may be provided with implementation-defined semantics):

Solution:

Escape
sequence
Description Representation
\' single quote byte 0x27 in ASCII encoding
\" double quote byte 0x22 in ASCII encoding
\? question mark byte 0x3f in ASCII encoding
\\ backslash byte 0x5c in ASCII encoding
\a audible bell byte 0x07 in ASCII encoding
\b backspace byte 0x08 in ASCII encoding
\f form feed – new page byte 0x0c in ASCII encoding
\n line feed – new line byte 0x0a in ASCII encoding
\r carriage return byte 0x0d in ASCII encoding
\t horizontal tab byte 0x09 in ASCII encoding
\v vertical tab byte 0x0b in ASCII encoding
\nnn arbitrary octal value byte nnn
\xnn arbitrary hexadecimal value byte nn
\unnnn (since C++11) universal character name
(arbitrary Unicode value);
may result in several characters
code point U+nnnn
\Unnnnnnnn (since C++11) universal character name
(arbitrary Unicode value);
may result in several characters
code point U+nnnnnnnn

Example:

Results:

Method 2: For dummies useful for sending Ctrl+Z in GSM modem SMS

In this method we separate all double quotes. Using single quotes to send double quotes. This method requires lot of coding. This is not really used.

This method is useful for some of the applications where both Method 1 and 3 will not work such as sending ctrl+z to terminate SMS in GSM modem interfacing, sending ASCII character codes with hex value.

Example 1: Serial.print(char(0x1A));  //Sends Ctrl+z

Example 2:

AT+CPMS=”SM”

Serial.print(“AT+CPMS=”);

Serial.print(‘”‘);

Serial.print(“SM”

Serial.println(‘”‘);

Method 3: All Escape

In ESP8266 when you want to send complete HTML page, It becomes difficult to use Method 1 and Method 2 to escape each double quotes and special characters.

This is my favorite method, which is not explained any where on internet, books. COOL, But it works like magic.

Use of Magic format const char myString = R”=====(    …your “string” data…    )=====”;

const char myString[] = R”=====(

your double quotes and “aksjdhk” aaskdj’asd’ {owiru } “blah”, “blah” ; goes here

)=====”;

These are the basic three methods used for escaping the double quotes in string.

 

 

Leave a Reply