Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ get the last (n) char in a string

Tags:

c++

string

i have a string and i want to get, for example, the position of the last (.) in the string, or whatever char i want to check, but untill now i just get a headeach.

thanks

like image 795
Castro Roy Avatar asked Dec 15 '10 22:12

Castro Roy


People also ask

How do you get the last n characters of a string?

To get the last N characters of a string, call the slice method on the string, passing in -n as a parameter, e.g. str. slice(-3) returns a new string containing the last 3 characters of the original string.

How do you find the last occurrence of a character in a string in C?

The strrchr() function in C/C++ locates the last occurrence of a character in a string. It returns a pointer to the last occurrence in the string. The terminating null character is considered part of the C string. Therefore, it can also be located to retrieve a pointer to the end of a string.

How do I get the last 3 characters of a string?

Simple approach should be taking Substring of an input string. var result = input. Substring(input. Length - 3);


1 Answers

string lastN(string input)
{
     return input.substr(input.size() - n);
}
like image 109
take-it Avatar answered Oct 14 '22 11:10

take-it