(1)string 类提供了 6 种查找函数
-
find
- Find content in string (public member function)
-
rfind
- Find last occurrence of content in string (public member function)
-
find_first_of
- Find character in string (public member function)
-
find_last_of
- Find character in string from the end (public member function)
-
find_first_not_of
- Find absence of character in string
-
find_last_not_of
- Find absence of character in string from the end (public member function)
(2)返回值
1.这些操作全都返回 string::size_type 类型的值,以下标形式标记查找匹配所发生的位置;
2. 或者返回一个名为 string::npos 的特殊值,说明查找没有匹配。string 类将 npos 定义为保证大于任何有效下标的值。npos = -1代表字符串索引号的末尾一个。或者表示无效索引号。
(3)用法举例
a:
string str;
pos=str.find_first_of("h");
if(str.find_first_of("h")!=
string::npos){
........
}
b:
确保str是正负数字
string str;
if(!str.empty()&& (str[0] == '-'|| isdigit(str[0])) &&str.find_first_not_of("0123456789",1)==std::string::npos)
{
.......
}