Browse - Programming Tips - How do I get the first character of a std::string ?

Date: 2012may4
Language: C/C++
Level: beginner

Q.  How do I get the first character of a std::string ?

A.  Like this:

std::string mystring; char first_char; first_char = mystring[0];
You can make function that does it:
inline char getfirstchar(std::string &s) { // If the string is empty this function will return \0 which // is good. So we don't need to do a special check for that. return s[0]; }