I wanna know if there is any standard function in visual stodio 2010, C++, which takes a character, and returns the index of it in special string, if it is exist in the string. Tnx
The indexOf() method returns the position of the first occurrence of specified character(s) in a string. Tip: Use the lastIndexOf method to return the position of the last occurrence of specified character(s) in a string.
To get the index of a character in a string, you use the indexOf() method, passing it the specific character as a parameter. This method returns the index of the first occurrence of the character or -1 if the character is not found.
The index() function locates the first occurrence of c (converted to an unsigned char) in the string pointed to by string. The character c can be the NULL character (\0); the ending NULL is included in the search. The string argument to the function must contain a NULL character (\0) marking the end of the string.
string::find() function returns the index of first occurrence of given substring in this string, if there is an occurrence of substring in this string. If the given substring is not present in this string, find() returns -1.
You can use std::strchr
.
If you have a C like string:
const char *s = "hello, weird + char.";
strchr(s, '+'); // will return 13, which is '+' position within string
If you have a std::string
instance:
std::string s = "hello, weird + char.";
strchr(s.c_str(), '+'); // 13!
With a std::string
you can also a method on it to find the character you are looking for.
strchr
or std::string::find
, depending on the type of string?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With