Sending a single double quote.

Good morning everyone.

I have a small problem that I'm hoping someone can help me resolve.

I'm collecting some weather data from Accuweather and one of the elements I'm collecting is a text field. This morning, the text field contained this -

A snow squall, up to 1"

Back in the day, ArduinoJson lib created this field for it -

const char* DailyForecasts_1_Day_ShortPhrase

When I saw the missing data and saw on the Accuweather site what had been sent, I knew what the problem was. The ".

This is my first C++ program and I went from Blink to 1200 lines of bad decisions. It seems like every line I've written took two days to debug.

I've tried this -

String t_temp = weather_forecast.DailyForecasts_Day_ShortPhrase;
t_temp.trim();
t_temp.replace("\"", "\\""");

Serial1.print("page4.t" + in + ".txt=\""); Serial1.printf("%s", t_temp); Serial1.print(F("\"")); strEnd();

And -

t_temp.replace("\"", "\"");

And -

t_temp.replace("\"", """");

Everything I've tried results in a line of garbage characters on the Nextion.

The Nextion print statement that I had been using before my testing was this-

Serial1.print("page4.t" + in + ".txt=\""); Serial1.printf("%s", weather_forecast.DailyForecasts_Day_ShortPhrase); Serial1.print(F("\"")); strEnd();

It works fine, except for not displaying anything if there's a " in the data.

Any suggestions would be greatly appreciated.

Between this project, the Stay-At-Home orders and my wife ragging on me for hoarding toilet paper, I'm about to completely lose it.

Thanks all.

You need one backslash to escape the backslash, and another one to escape the double quote:

t_temp.replace("\"", "\\\"");

Alternatively, use a raw string literal so you don't need to escape anything:

t_temp.replace(R"(")", R"(\")");

Two string literals next to each other are concatenated. E.g. "ab" "cd" is equivalent to "abcd". Your code had "\""", which is equivalent to "\" "" (a single back slash and an empty string), so it's just "\".

Pieter

Thank you Pieter.

I used R"" and it worked fine.

I also had to change the printf("%s", t_temp) to a simple print(t_temp) since it was rendering every line into garbage.

My homework for tonight - Read up on R and why "%s" doesn't render String.

Attached was the offending screen, now rendered correctly.

Jim

Printf's %s format option expects a pointer to a null-terminated string, not a String object. If you want to use printf, you'll have to use t_temp.c_str().