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:

"AT+CPMS=\"ME\""
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:

/*
* Arduino String Escape 
* https://cirucits4you.com
*/
void setup()
{
   Serial.begin(9600);
}
void loop()
{
    Serial.print("This\nis\na\ntest\n\nShe said, \"How are you?\"\n");
    delay(1000);
}

Results:

This
is
a
test

She said, "How are you?"

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

)=====”;

const char MAIN_page[] = R"=====(
<!DOCTYPE html>
<html>
<head>
<title>ESP8266 Servo | Circuits4you.com</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<style>
.imageDiv{
    padding: 5%;
 }

.flx{
  display: flex;
}

</style>
<body>
<div style="width:100%;">
<div style="width:50%;  margin: 0 auto;">
  <h3>Circuits4you.com</h3>
  <h4>ESP8266 Double quotes escape Demo</h4>
</div>
</div>
</body>
</html>
)=====";

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

 

 

Leave a Reply