在C ++中查找字符串中特殊字符的索引

rai*_*ain 5 c++ string visual-studio-2010 visual-c++

我想知道Visual stodio 2010 C ++中是否有任何标准函数,该函数需要一个字符,并在特殊字符串中返回它的索引(如果字符串中存在)。特纳克斯

Pab*_*ruz 5

您可以使用std::strchr.

如果你有一个类似C 的字符串:

const char *s = "hello, weird + char.";
strchr(s, '+'); // will return 13, which is '+' position within string
Run Code Online (Sandbox Code Playgroud)

如果您有一个std::string实例:

std::string s = "hello, weird + char.";
strchr(s.c_str(), '+'); // 13!
Run Code Online (Sandbox Code Playgroud)

使用 astd::string您还可以使用一种方法来查找您要查找的字符。