c++中字符串的使用--------string::npos 的理解
2013-04-03 21:44
711 查看
static const size_t npos = -1;
The string class provides six search functions, each named as a variant offind. The operations
all return astring::size_type value that is the index of where the match occurred, or a special value namedstring::npos if there is no match. Thestring
class defines npos as a value that is guaranteed to be greater than any valid index.
翻译如下:
string 类提供了 6 种查找函数,每种函数以不同形式的 find 命名。这些操作全都返回 string::size_type 类型的值,以下标形式标记查找匹配所发生的位置;或者返回一个名为 string::npos 的特殊值,说明查找没有匹配。string 类将 npos 定义为保证大于任何有效下标的值。
int main(){
string strinfo="//*------------------HelloWorld...........-----";
string strset="ABCDEFHabcd";
string::size_type first=strinfo.find_first_of(strset);
if (first==string::npos)
cout<<"not find any characters"<<endl;
string::size_type last=strinfo.find_last_of(strset);
if (last==string::npos)
cout<<"not find any characters"<<endl;
cout<<strinfo.substr(first,last-first+1)<<endl;
return 0;
}
运行结果是:HelloWorld
The string class provides six search functions, each named as a variant offind. The operations
all return astring::size_type value that is the index of where the match occurred, or a special value namedstring::npos if there is no match. Thestring
class defines npos as a value that is guaranteed to be greater than any valid index.
翻译如下:
string 类提供了 6 种查找函数,每种函数以不同形式的 find 命名。这些操作全都返回 string::size_type 类型的值,以下标形式标记查找匹配所发生的位置;或者返回一个名为 string::npos 的特殊值,说明查找没有匹配。string 类将 npos 定义为保证大于任何有效下标的值。
int main(){
string strinfo="//*------------------HelloWorld...........-----";
string strset="ABCDEFHabcd";
string::size_type first=strinfo.find_first_of(strset);
if (first==string::npos)
cout<<"not find any characters"<<endl;
string::size_type last=strinfo.find_last_of(strset);
if (last==string::npos)
cout<<"not find any characters"<<endl;
cout<<strinfo.substr(first,last-first+1)<<endl;
return 0;
}
运行结果是:HelloWorld
相关文章推荐
- [C/C++标准库]_[初级]_[使用模板删除字符串前后空格((w)string space)]
- C++中string的用法 string字符串的使用方法
- [C/C++标准库]_[初级]_[使用模板删除字符串前后空格((w)string space)]
- c++中string类型字符串的使用
- Java中JNI的使用详解第四篇:C/C++中创建Java对象和String字符串对象及对字符串的操作方法
- C++中string的用法 string字符串的使用方法
- 实战c++中的string系列--十六进制的字符串转为十六进制的整型(一般是颜色代码使用)
- C++的中英文字符串表示(string,wstring),使用wcout.imbue(std::locale("chs"));本地化解析编码
- 实战c++中的string系列--十六进制的字符串转为十六进制的整型(通常是颜色代码使用)
- C++中string字符串使用
- 实战c++中的string系列--十六进制的字符串转为十六进制的整型(通常是颜色代码使用)
- c风格字符串和C++的string在文件名方面的使用注意
- 01 怎样在C++/CLI中使用资源文件里的String Table中字符串对应的值
- C++ 区分中文,非中文,截取含有中文的string字符串 方法汇集
- 在framework层中使用string字符串
- C++中string::find()函数和string::npos函数的使用
- C++字符串使用sizeof时注意
- C++字符串:string and wstring的区别,非常重要!!!!
- C++字符串对象(string)
- C/C++字符串处理盘点:Char*/String/StringBuilder/TextPool/Rope