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 }

 

posted @ 2020-02-17 21:06  txwtech  阅读(160)  评论(0编辑  收藏  举报