Dave's Brain

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]; }

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.