Dave's Brain

Browse - programming tips - c++ get last character of std string

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

Q.  How do I get the last charater 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];
	}

What this info useful to you? You can donate to say thanks

Add a comment

Sign in to add a comment
Copyright © 2008-2010, 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.