cb20a_c++_string类型的查找
cb20a_c++_string类型的查找
s.find(args) //精确匹配,顺序查找, abc, 连续的包含在abcde,或者fabcde;
s.rfind(args) //精确匹配。反向查找
s.find_first_of(args)//不连续,间隔的,一个一个的找,比如扎到a就返回位置。
s.find_last_of(args)//反向查找
s.find_first_not_of(args)//不连续,间隔的,一个一个的找,比如知道非a就返回非a的位置,就是除了a,其他都返回找到了。
s.find_last_not_of(args)//反向查找
欢迎讨论,相互学习。 txwtech@163.com
1 /*cb20a_c++_string类型的查找 2 s.find(args) //精确匹配,顺序查找, abc, 连续的包含在abcde,或者fabcde; 3 s.rfind(args) //精确匹配。反向查找 4 s.find_first_of(args)//不连续,间隔的,一个一个的找,比如扎到a就返回位置。 5 s.find_last_of(args)//反向查找 6 s.find_first_not_of(args)//不连续,间隔的,一个一个的找,比如知道非a就返回非a的位置,就是除了a,其他都返回找到了。 7 s.find_last_not_of(args)//反向查找 8 9 欢迎讨论,相互学习。 txwtech@163.com 10 */ 11 #include <iostream> 12 #include <string> 13 14 using namespace std; 15 16 int main() 17 { 18 string name("AnnaBelle"); 19 string::size_type pos1=name.find("nna");////精确匹配 20 cout << "如果找到:返回下标:" << pos1 << endl; 21 if (pos1 == string::npos) 22 cout << "如果npos,表示没有找到" << endl; 23 else 24 cout << "找到了下标: " << pos1 << endl; 25 26 name = "r2%d3"; 27 string numerics("0123456789"); 28 string::size_type pos=name.find_first_of(numerics); 29 cout << "找name里面的数字,在numerics里面包含有。找到2,就找到了,后面不找了" <<pos<< endl; 30 if (pos == string::npos) 31 cout << "如果npos,表示没有找到" << endl; 32 else 33 cout << "找到了下标: " << pos1 << endl; 34 string::size_type pos3 = 0; 35 while ((pos3 = name.find_first_of(numerics, pos3)) != string::npos)//用循环把所有找出来 36 { 37 cout << "找到了数字,内容是: " << name[pos3] <<endl<< endl; 38 ++pos3; 39 } 40 cout << "找出不是数字的方法:" << endl; 41 pos3 = 0; 42 while ((pos3 = name.find_first_not_of(numerics, pos3)) != string::npos)//用循环把所有找出来 43 { 44 cout << "找不是数字,内容是: " << name[pos3] << endl<<endl; 45 ++pos3; 46 } 47 string letters("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"); 48 pos = 0; 49 while ((pos = name.find_first_of(letters, pos)) != string::npos) 50 { 51 cout << "letters里面找到了字母:"<<name[pos] << endl; 52 ++pos; 53 } 54 cout << "找出不是字母的方法:" << endl; 55 pos = 0; 56 while ((pos = name.find_first_not_of(letters, pos)) != string::npos) 57 { 58 cout << "letters里面找到了非字母的其他字符:" << name[pos] << endl<<endl; 59 ++pos; 60 } 61 62 string river("Mississippi"); 63 string river2("a2sipii"); 64 string::size_type first_pos = river.find("is"); 65 cout << "first_pos前面开始找:下标是:"<< first_pos << endl; 66 string::size_type last_pos = river.rfind("is"); 67 cout << "last_pos后面开始找:下标是:" << last_pos << endl << endl; 68 69 70 //name = "r2%d3"; 71 //string numerics("0123456789"); 72 pos = name.find_last_of(numerics); 73 cout << "name在numerics反向查找位置的索引:" << pos << endl << endl; 74 75 76 77 78 79 return 0; 80 }
欢迎讨论,相互学习。
cdtxw@foxmail.com
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)