How can I get a character in a string by index?

SmartestVEGA picture SmartestVEGA · Mar 10, 2010 · Viewed 123.6k times · Source

I know that I can return the index of a particular character of a string with the indexof() function, but how can I return the character at a particular index?

Answer

Tim Robinson picture Tim Robinson · Mar 10, 2010
string s = "hello";
char c = s[1];
// now c == 'e'

See also Substring, to return more than one character.