Constructed String does not allow well functioning Stringoperations

Hello, I am reading from my serial and write / collect it to a string. AUnfortunaly i can not work with stringoperaters afterwards. I made a tiny debug-version with some (mainly) outputs to see what I mean.

I am getting crazy - did I do something wrong? and if so What did I forget to google for ? ;) Tbh: I have no Idea Please have a look at my code and take for Example aa11aa as input

#include "Arduino.h"

String readString;
bool lineComplete = 0;

void setup() {
  Serial.begin(9600);
  Serial.println("serial initialized"); 
}

void loop() 
{
  while ( (Serial.available()>0)  & (!lineComplete) )
  {
    char c = Serial.read();
    if (c == 10 )
    {
      lineComplete=true;
      Serial.print("linecomplete; Readstring ln = ");
      Serial.println(readString.length());
    }
    else
    { 
      readString+=c; 
    }

  }
  if (lineComplete)
  {    
    if (readString.length() >0) 
    {
      readString.trim();
      
      Serial.print("*****String: ");
      Serial.println(readString);

      Serial.print("Length: ");
      Serial.println(String(readString.length()));

      Serial.print("String(readString[0]): ");
      Serial.println(String(readString[0]));
      // prints the correct 1'st char of the String

      Serial.print("is myString equals 'a' on Position 0? ");
      Serial.println(readString[0]=='a');
      // prints 0 but should be 1

      Serial.print("is myString equals ''a'' on Position 0? ");
      Serial.println(String(readString[0])=="a");
      // prints 0 but should be 1

      Serial.print("LastIndex of ''a''? ");
      Serial.println(String(readString.lastIndexOf("a")));
      // prints -1 but should be >0
      // and so on with other string operators or even whole strings with more than one character


      lineComplete=false;
    }
  }
}

Thanks Nils

Please have a look at my code and take for Example aa11aa as input

You're the one with the Arduino, the sample input, and the problem. You show us your output.

Yellow is a crappy color. That text is completely unreadable.

      Serial.println(String(readString[0]));

There is no reason to create a String to print one character.

      Serial.print("*****String: ");
      Serial.println(readString);

This won't tell you a lot about what is in the String object.

      Serial.print("String: [");
      Serial.print(readString);
      Serial.println("]");

would be a lot better.

PaulS:
Yellow is a crappy color. That text is completely unreadable.

==> I chnaged it :zipper_mouth_face:

PaulS:
There is no reason to create a String to print one character.

The casting was only a test from former times - you are right

PaulS:
This won’t tell you a lot about what is in the String object.
[…]would be a lot better.

Good Idea to make it better visible with brackets-

As far as I undersood you’d like to see my output explicitly - here we go

linecomplete; Readstring ln = 6<\r><\n>
*****String:[aa11aa]<\r><\n>
Length:6<\r><\n>
String(readString[0]): a<\r><\n>
is myString equals 'a' on Position 0? 0<\r><\n>
is myString equals ''a'' on Position 0? 0<\r><\n>
LastIndex of ''a''? -1<\r><\n>

As far as I undersood you'd like to see my output explicitly - here we go

Next step is to add some debug statements to WString.cpp, and see what is happening in the class.

Don't do this, though:

      Serial.println(String(readString.length()));

The Serial class derives from Stream which derives from Print. The Print class has an overloaded version of println() that knows how to print an integer. Making a String of the int is not necessary.

This concerns me though:

I made a tiny debug-version with some (mainly) outputs to see what I mean.

With two major oops in that little bit of code, it tells me that you don't understand what a resource hog the String class is, or how it can fragment memory, leading to all kinds of problems.

Everything that the String class can do can be done with char arrays and string functions (from string.h). At a far lower overall cost.

PaulS:
Next step is to add some debug statements to WString.cpp, and see what is happening in the class.

Wow - unfortunately I am not so deep in c++ (this is quite a long time ago) - If you could introduce me I would appreciate

Don’t do this, though:
[…]The Serial class derives from Stream which derives from Print. The Print class has an overloaded version of println() that knows how to print an integer. Making a String of the int is not necessary.

good to know - thx

Everything that the String class can do can be done with char arrays and string functions (from string.h). At a far lower overall cost.

no- I did not know that. OK I tried to solute this via an char-array parallel now - with the same effect -

#include "Arduino.h"
#include "string.h"

String readString;
char readChar[11];
int charCounter = 0;
bool lineComplete = 0;

void setup() {
  Serial.begin(9600);
  Serial.println("serial initialized"); 
}

void loop() 
{
  while ( (Serial.available()>0)  & (!lineComplete) )
  {
    char c = Serial.read();
    if (c == 10 )
    {
      lineComplete=true;
      Serial.print("linecomplete; Readstring ln = ");
      Serial.println(readString.length());
    }
    else
    { 
      
      readString+=c; 
      readChar[charCounter]=c;
      charCounter++;
    }

  }
  if (lineComplete)
  {	
    if (readString.length() >0) 
    {
      
      readChar[charCounter+1]=0;
      readString.trim();
      
      Serial.print("*****String:[");
      Serial.print(readString);
      Serial.println("]");
      Serial.print("*****CharArr:[");
      Serial.print(readChar);
       Serial.println("]");

      Serial.print("Length of String:");
      Serial.println(readString.length());
      Serial.print("Length of CharArr:");
      Serial.println(strlen(readChar));
      Serial.println();

      Serial.print("String(readString[0]): ");
      Serial.println(String(readString[0]));
      Serial.print("CharArr[0]:");
      Serial.println(readChar[0]);
      Serial.println();
      // prints the correct 1'st char of the String

      Serial.print("is myString equals 'a' on Position 0? ");
      Serial.println(readString[0]=='a');
      Serial.print("Is myChar equals 'a' on Position 0?");
      Serial.println(readChar[0]=='a');
      Serial.println();
      // prints 0 but schould be 1

      Serial.print("is myString equals ''a'' on Position 0? ");
      Serial.println(String(readString[0])=="a");
      // prints 0 but schould be 1

      Serial.print("Is myChar containing 'a' ?");
      Serial.print(strchr(readChar,'a')));
      Serial.println();

      Serial.print("String Index ''a''? ");
      Serial.println(readString.indexOf("a"));
      // prints -1 but should be >0
      Serial.print("Char FirstIndex of ''a''? ");
      
      Serial.println(strstr(readChar,"a"));
      
      // andso on


      lineComplete=false;
      charCounter=0;
    }
  }
}

The output on “11aa11” is:

linecomplete; Readstring ln = 6<\r><\n>
*****String:[11aa11]<\r><\n>
*****CharArr:[11aa11]<\r><\n>
Length of String:6<\r><\n>
Length of CharArr:6<\r><\n>
<\r><\n>
String(readString[0]): 1<\r><\n>
CharArr[0]:1<\r><\n>
<\r><\n>
is myString equals 'a' on Position 0? 0<\r><\n>
Is myChar equals 'a' on Position 0?0<\r><\n>
<\r><\n>
is myString equals ''a'' on Position 0? 0<\r><\n>
Is myChar containing 'a' ?0<\r><\n>
String Index ''a''? -1<\r><\n>
Char FirstIndex of ''a''? 0<\r><\n>

SOLUTION

This strange behavior was gone after I moved my Dev-Environment and Sketchbook away from my SD-Card to my harddrive I can not explain why - but it works !

I can not explain why

I can't even imagine why.