zoukankan      html  css  js  c++  java
  • 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 }
    欢迎讨论,相互学习。 txwtech@163.com
  • 相关阅读:
    UEditor中多图上传的bug
    Mysql 5.5 数据库命令大全
    Android学习书籍
    众方中继使用说明
    MyEclipse 2015优化技巧
    Android studio1.3.1 快捷键
    你有没有这样的猜疑?
    右键新建没有Word_Excel_PowerPoint的解决方法
    电脑清理桌面快捷图标小箭头
    Spring3.1+Hibernate3+Struts2的最新整合所需要的jar包
  • 原文地址:https://www.cnblogs.com/txwtech/p/12323653.html
Copyright © 2011-2022 走看看