Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
395 views
in Technique[技术] by (71.8m points)

arduino - Add quotes to start and end of String

I'm working with a project that consist on sending data that I receive from the console terminal from Arduino IDE. I have been able to get the data that I have typed inside the console and store on a variable. My variable is a String, when I get the data that I have typed from the console, it comes like this: This is text. I would need to concat one quote to the start and one to the end, so it will stay like: "This is text". I would need that because to write the data inside esp32 SPIFFS it has to be inside quotes.

String wifi_name;

if (Serial.available()){
      wifi_name = Serial.readString();
    }
    
    if (wifi_name == NULL){
      Serial.print("File size: ");
      Serial.print(file.size());
      Serial.println(", O valor dentro da string é nulo, nada será adicionado ao arquivo");
      delay(2500);
    }else{
      write_file_info(wifi_name);
    }
void write_file_info(String message) {
  file = SPIFFS.open("/wifi.txt", FILE_WRITE);

  if (!file){
    Serial.println("Error opening file");
    return;
  }else{
    Serial.println("Success opening file");
  }

  if (file.print("message")){           //This line I would need to have quotes in the end and start
    Serial.println("File was written");
  }else{
    Serial.println("File was not written");
  }

  file.close();
}

I would need that the message part if (file.print("message")) stayed between quotes, but since it is a String, I can't apply the quotes.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

https://www.arduino.cc/en/Tutorial/BuiltInExamples/StringAdditionOperator And to concat special character like " you can use escape character like this """ + text + """ .


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...