Print String with apostrophe at the end

EDITED:

I want to print this on one line:

cmd = Serial.readString();

if (cmd.indexOf("PASSWORD:") > -1) {
        password = cmd.substring(9);
       Serial.println(password);
      delay(100);
    Serial.println("AT+PSWD=" "\"" + password + "\"");
}

but I got this:

10:37:10.796 -> AT+PSWD="1234
10:37:10.796 -> "

What is the problem?

Try:


Serial.print("AT+PSWD=");

Serial.print(password);

Serial.println("’");

Guessing, password contains a linefeed after the number, likely if it was read from the serial input.

yes, it was read from the serial. What is the solution for this?

https://www.arduino.cc/reference/en/libraries/streaming/

-- and --

http://arduiniana.org/libraries/streaming/

Serial << "AT" << "PSWD=" << password << "'" << endl;
1 Like

The apostrophe is still on the second line

Honestly, I never used Serial.readstring() rather using an input like:

do{
    if(Serial.available()>0){
      input = Serial.read(); 
      cArray[i]=input;
      i++;
    }
  }while(input != '\r');

Maybe try trimming your password variable:

trim() - Arduino Reference

I got an idea from trim.

int passwordlength = cmd.length(); 
password = cmd.substring(9,passwordlength-2);

Thanks!

As long as you are happy!
Good luck!

Display with Streaming.h macro, it will simply your printing and it is a macro, so no program space is required, all happens with preprocessor.

Ray

This:

     "AT+PSWD=" "\"" + password + "\""

Is not this, what you meant?

  "AT+PSWD="   +   "\""     +    password  +   "\""

You left out an operator. +

a7

The first relies on the compiler, which will combine the two quoted texts together into a single text. This compiler behavior is convenient when embedding a hex value into a text literal using \x to prevent the subsequent ASCII characters from being interpreted as part of the hex value:

"test \x0A" "feedback" // without the extra quotes the entire next word is taken as part of the hex value

< edit >
Unless you absolutely need to have a single text string, I much prefer just printing each part individually, even with the fastest serial speed the receiving device will never see any difference.

  Serial.print("AT+PSWD=\"");
  Serial.print(password);
  Serial.println("\"");

And I miss printf. Well, I don't, because I found a short easy way to get it back.

But THX for 'splaining the " implicit string literal concatenation", file that under learned something today!

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.