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

Date: 2010may14
Language: C/C++
Level: beginner

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

A.  Like this:

std::string mystring; char last_char; last_char = mystring[mystring.length() - 1];
You can make function that does it:
inline char getlastchar(std::string &s) { if (s.length() == 0) return '\0'; return s[s.length() - 1]; }