Pages: [1]   Go Down
Author Topic: charAt()  (Read 402 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 93
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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()
{
}
Logged

Offline Offline
Edison Member
*
Karma: 17
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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)
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 167
Safety first.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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()
{
}
Logged

Pages: [1]   Go Up
Jump to: