c++ string类的find:
1.find
string s = "My cat's breath smells like cat food.";
int a=s.find("breath");
cout<<a<<endl;
因为find是从0数起,输出字符串第一个字符的索引(从0开始),所以a为9
2.rfind
string s = "My cat's breath smells like cat food.";
int a=s.rfind("breath");
cout<<a<<endl;
因为rfind是从后部数起,还是输出字符串第一个字符的索引(从0开始)
其实,find与rfind一样,都可以找完整的字符串和字符,下面来看看另外一些奇葩的函数
3.find_first_of
string s = "My cat's breath smells like cat food.";
int a=s.find_first_of("beah");
cout<<a<<endl;
从0数起,但它只需找到一个与它串里一样的字符就输出来了
答案是:4
4.find_last_of
string s = "My cat's breath smells like cat food.";
int a=s.find_last_of("beah");
cout<<a<<endl;
从后部数起,而且和find_first_of一样,输出最后一个有一样的就好了
答案是: 29
5.find_first_not_of
string s = "aaabaaaaaabaa.";
int a=s.find_first_not_of('a');
cout<<a<<endl;
从0数起,但它只需找到一个与它串里不一样的字符就输出来了
答案是:3
6.find_last_not_of
string s = "aaabaaaaaabaa";
int a=s.find_last_not_of('a');
cout<<a<<endl;
从后部数起,而且和find_first_not_of一样,也是输出第一个(似乎是最后一个哈)有一样的就好了
答案是: 10
如果没找到的话会输出string::npos
原文链接: https://www.cnblogs.com/hkaren/p/12356723.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/193513
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!