String.replace() issues, doesn't work

Hey there. Currently making a universal global buffer for my ESP32-S3

It utilizes both char and String, char to fully receive serial data, String to store it in a readable and simple form.

One thing before you rush to comments, I absolutely don't care how bad String is with memory, don't recommend me to use c-string, thank you very much.

Part of my code:

String serialbuffer = "";

void setup() {
...
ESP assigns core 1 and core 2 functions
}

void loop() {
--Nothing here--
}

void core1() {
...
}

void core2() {
...
}

int xprocess(String arg) {
  Serial.println("SBF: |" + serialbuffer + "|\nCompare: |" + arg + "|");
  int dat = serialbuffer.indexOf(arg);
  if (dat > -1) {
    Serial.println("Replacing");
    serialbuffer.replace(arg, "");
  }
  return dat;
}

void xreceive() {
  char* serialbuffertemp = "";
  while (sysPort.available()) {

    if (serialbuffer.length() > 1023) {
      serialbuffer = "";
    }
    strcat(serialbuffertemp, Serial.readString().c_str());
  }
  serialbuffer.concat(String(serialbuffertemp));
  serialbuffer.trim();
}

xreceive - Receives available data from Serial, stores into global String buffer. Note if the global String buffer is 1024 bytes or more, it will be emptied.
xprocess - Uses the provided String parameter and checks if it exists within the main buffer, if so, removes that part of text.

I tried using Serial.readString() directly, however with doing serialbuffer.concat(Serial.readString()), neither .indexOf, neither .replace worked...

Using this code, .indexOf works, however .replace doesn't.

ESP32 2.0.5 installed.
ESP32-S3-WROOM-1-N8 module used.

Help very much appreciated!

I think that in this case you yourself will solve your problem better than the forum

Can you show a small working sketch that shows the problem and that we can try ? thank you very much :clown_face:

I think that you have to write easy and simple code. Don't try to put a multitude of functionality into a single line.
Others might see other problems, but this code line got my attention at first glance, because it is over-complicated :

strcat(serialbuffertemp, Serial.readString().c_str());

Looking at it better, I think that is a bug. You have reserved one byte in memory for the contents of 'serialbuffertemp', and then you write the received text to it.

char* serialbuffertemp = "";

This is a worse error than just a size. It is more important that this is not a text buffer, but a pointer to a literal. A literal is not meant to be modified, so the compiler may not allocate memory for it, but place a pointer to any other string with the same content.
For example, these two strings will actually share the same place in memory:

char* a = "ABCDE";
char* b = "CDE";

Since the line char* serialbuffertemp = ""; points to the string terminator char, which is in any other string in the program, than most likely the compiler will not allocate even 1 character for this line.

1 Like

Bugs are never where you think they are (as pointed out by @Koepel ) :slight_smile:

Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advice on) your project :wink: See About the Installation & Troubleshooting category.

No preaching here, but you can do a lot more, faster, in less memory with c-strings.

Mixing strings and Strings is a real deathwish for performance and reliability.

Choose one , and learn about it.

How about this part:

while (sysPort.available()) {
  ... Serial.readString()...
}

That is like saying "Who is there" when someone knocks on the neighbors door.

Thanks for the heads up about the categories!

Written as a precaution in case the secondary accessory connected via Serial is in progress of sending data while reading the buffer.

Got it, so I should allocate 1024 bytes to it for example.

So it should be char serialbuffertemp[1024] = "";?

I agree, however I couldn't find any proper methods to remove a word from a c-string.

Let me clarify, the plan is like this:

  • Get data from serial
  • Check if a word trigger exists in the buffer, if so, remove it from the buffer and continue to the requested function.

I have tried using String only, however as I mentioned in my original post, neither .replace() nor .indexOf() work when doing serialbuffer.concat(Serial.readString())

However both functions do work if I just do serialbuffer = Serial.readString()

Yeah it is over-complicated since as mentioned in my original post, and once more in post #11:

Neither .replace() nor .indexOf() work when doing serialbuffer.concat(Serial.readString())

However both functions do work if I just do serialbuffer = Serial.readString()

As well as

I couldn't find any proper methods to remove a word from a c-string.

Quick update:

Intializing the temporary char by doing char serialbuffertemp[1024] = "" fixed the .replace issue.

For those wondering why 1024, I have set the ESP serial buffer to 1024 bytes total in HardwareSerial.

That’s true, but there many online examples of short functions to insert, replace, or remove c-substrings.

Agreed, I made a mash up of those and it kind of worked but the whole code was so complicated and messy I just couldn't handle using it in my project, plus there were too many concerns about the stability.

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