Adding chars at certain points in a string

Hi Guys,

I am busy doing a project that requires a lot of serial data parsing and I have found myself in a sticky situation.

Does anyone know a trick to insert characters in a string at certain points without replacing other chars. For example.

My string looks like this:

-37.71-43.05-41.03-47.34-45.83-44.59

I need to place a comma at every "-" sign, to separate the bits of data. So it needs to looks like this:

-37.71,-43.05,-41.03,-47.34,-45.83,-44.59

Any String parameters i might be missing to make this happen?

Also, I can't hard code it to make it insert a comma every 6 chars because the data bits could be all different lengths. I need to basically search for every "-" in the string and place "," before it.

Any help would be greatly appreciated! Thanks!

You'll need to copy it to another char array, inserting the extra characters as you do the copy.

Regards,
Ray L.

Hi Ray,

Thanks for the point in the right direction.

I thought it would have to be solved with something to do with using an arrray.

Do you have any example of how to do this? The Char Array examples found online don't help too much.

Thanks again!

Regards,

Where is that data going?

Do you really need to keep a copy?

I am busy doing a project that requires a lot of serial data parsing and I have found myself in a sticky situation.

Parsing is the decomposition of string data, not the formatting, what problem do you really want to solve?

BTW your specification is not correct

-37.71-43.05-41.03-47.34-45.83-44.59

I need to place a comma at every "-" sign, to separate the bits of data. So it needs to looks like this:

would result in

,-37.71,-43.05,-41.03,-47.34,-45.83,-44.59

Hey Guys,

Thanks so much for the help.

I finally figured it out.

@Whandall. I see what you mean, It would include the "," in the front as well.

To clarify what exactly what is happening in my situation for anyone else having this problem.

I was receiving data from the serial in the format:

-37.71-43.05-41.03-47.34-45.83-44.59

From a sensor.

all I needed to do was to save it into a string with the correct format which was having the points of data seperated by commas (for .CSV) which is (as Whandall has corrected me):

,-37.71,-43.05,-41.03,-47.34,-45.83,-44.59

I fixed this by passing the Serial.read to char, look for "-", add a "," and the pass it to a String.
like this:

void loop() {

        // Type anything into the Serial Monitor and when you include a "-", it will add a "," before it.
        while (Serial.available()) {
        delay(1);     //small delay to allow input buffer to fill
      if (Serial.available() >0) {
        char c = Serial.read();  //gets one byte from serial buffer
        if (c == '-'){          // looks for the - sign
        readString += ",";      // adds the "," to the string before the sign          
            }
        readString += c;       // adds the rest of the string.
        }
      
      if (readString.length() >0) {
      Serial.print(readString);   //prints string to serial port out
      
      readString=""; //clears variable for new input
      }
    }
}

I hope this helps anyone else who comes across this issue!

Thanks again.

Regards,

Be careful with your use of the terms String and string. They are not the same thing at all.