I have a string,
char* str = "HELLO"
If I wanted to get just the E
from that how would I do that?
To get the nth character in a string, simply call charAt(n) on a String , where n is the index of the character you would like to retrieve. NOTE: index n is starting at 0 , so the first element is at n=0.
char* str = "HELLO";
char c = str[1];
Keep in mind that arrays and strings in C begin indexing at 0 rather than 1, so "H" is str[0]
, "E" is str[1]
, the first "L" is str[2]
and so on.
You would do:
char c = str[1];
Or even:
char c = "Hello"[1];
edit: updated to find the "E".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With