Go Down

Topic: charAt() (Read 493 times) previous topic - next topic

N314

Hi

Ive been trying t learn string commands. Whenever I use "charAt()" the IDE does not highlight he word and ignores the command. Is there somthing im missing?

Code: [Select]
String string1 = "This is string1";
String string2 = "This is string2";
String char4 = "The 4th Char in string1 is ";
String charat4;

void setup()
{
  Serial.begin(9600);
  Serial.println(string1);
  Serial.println(string2);
  Serial.print(charat4);
  char4 = String(string1.charAt(4));
  Serial.println(char4);
}

void loop()
{
}

WizenedEE

Yes, an entry in the lib/keywords.txt file.

(Assuming it compiles, of course -- I'm not sure if it's a valid command or not, but if it compiles it certainly is)

Catcher

It's zero indexed.
Your program will not do what you want it to, but you have the right idea.
So when you ask for spot 4, you think you'll get an "s", but rather you'll get a space because it counts starting at 0 and index 4 is a " ".

This may be what you meant:
Code: [Select]

String string1 = "This is string1";
String string2 = "This is string2";
String prompt = "The 4th Char in string1 is ";
Char charAt4;
//String charAt4;

void setup()
{
 Serial.begin(9600);
 Serial.println(string1);
 Serial.println(string2);
 Serial.print(prompt);
 charAt4 = string1.charAt(3); //Prints 4th spot, the s
 //charAt4 = (String)string1.charAt(3);
 Serial.println(charAt4);
}

void loop()
{
}

Go Up