Unable to clear the string

Hello to everyone :slight_smile:
I am writing a code that is very simple but I have some problems with strings. Here is the code:

boolean stringComplete = false;
String inputString = "";

void setup() {
  Serial.begin(9600);
  inputString.reserve(20);
}

void loop() {

  if(stringComplete){
    
    Serial.println(inputString);
    Serial.println(inputString.length());
    
    Serial.println(inputString[0]);
    Serial.println(inputString[1]);
    Serial.println(inputString[2]);
    Serial.println(inputString[3]);
    Serial.println(inputString[4]);    
    
    inputString = "";
    
    stringComplete = false;
  }
}


void serialEvent() {
  while (Serial.available()) {

    char inChar = (char)Serial.read(); 

    inputString += inChar;
   
    if(inChar == '>') {
      stringComplete = true;
    } 
  }
}

From another Arduino Uno I am sending 5 bytes like this for example: . Only when first string is completed I get correct string which length is 5. But all the others strings are length of 7, somehow I get additional two chars in the begining of the received string.
Lines:

    Serial.println(inputString[0]);
    Serial.println(inputString[1]);
    Serial.println(inputString[2]);
    Serial.println(inputString[3]);
    Serial.println(inputString[4]);

are giving me the next result:

<
A
2

Can you see the solution to this problem? Thanks in advance!

First, your post title is wrong. Second, the body of your post contains many errors. You are not having problems with strings. You are having problems due to misusing Strings.

A String is an object that wraps a NULL terminated array of chars. The wrapper offers little benefit, but causes many problems.

So NULL terminated array means that the NULL is a stop sign. Stop processing the data in the array when you get here, You failed to stop at the stop sign.

Thank you for your answer, I see it more clearly now. I am sorry for mistakes that I have made, next time I will be more carefull :slight_smile:

next time I will be more careful

Especially stopping at stop signs. Blowing a stop sign could get you killed.

PaulS:
A String is an object that wraps a NULL terminated array of chars.

O.M.G!!! The light is coming on for PaulS! 8)

Not sure what your end product is going to be, but below is a method of capturing a string of characters sent from the serial monitor using a comma to indicate the end of the characters being sent.

//zoomkat 3-5-12 simple delimited ',' string  
//from serial port input (via serial monitor)
//and print result out serial port

String readString;

void setup() {
  Serial.begin(9600);
  Serial.println("serial delimit test 1.0"); // so I can keep track of what is loaded
}

void loop() {

  //expect a string like wer,qwe rty,123 456,hyre kjhg,
  //or like hello world,who are you?,bye!,
  
  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      //do stuff
      Serial.println(readString); //prints string to serial port out
      readString=""; //clears variable for new input      
     }  
    else {     
      readString += c; //makes the string readString
    }
  }
}

O.M.G!!! The light is coming on for PaulS!

Oh geez. You leave out "uselessly" once and some people think that means you accepts crutches as valid.

zoomkat:
O.M.G!!! The light is coming on for PaulS! 8)

Better read this, zoomkat:

Method      Time   Memory   Sketch
¬† ¬† ¬† ¬† ¬† ¬† ¬† ¬ĶS¬† ¬† Used¬† ¬†  Size
            
C-string      44     517      1782
STL string   468     554      2994
String      2480     526      3746

So, String is fine, if you don't mind it taking 2480/44 times as long (56 times as long). And risk memory fragmentation.