zoukankan      html  css  js  c++  java
  • C++ string中find() ,rfind() 等函数 用法总结及示例

    string中 find()的应用  (rfind() 类似,只是从反向查找)
    原型如下:
    (1)size_t find (const string& str, size_t pos = 0) const;  //查找对象--string类对象
    (2)size_t find (const char* s, size_t pos = 0) const; //查找对象--字符串
    (3)size_t find (const char* s, size_t pos, size_t n) const;  //查找对象--字符串的前n个字符
    (4)size_t find (char c, size_t pos = 0) const;  //查找对象--字符
    结果:找到 -- 返回 第一个字符的索引
         没找到--返回   string::npos
     
    示例:
     1  
     2 #include <iostream> // std::cout
     3  
     4 #include <string> // std::string
     5  
     6  
     7  
     8 int main ()
     9  
    10 {
    11  
    12 std::string str ("There are two needles in this haystack with needles.");
    13  
    14 std::string str2 ("needle");
    15  
    16  
    17  
    18 // different member versions of find in the same order as above:
    19  
    20 std::size_t found = str.find(str2);
    21  
    22 if (found!=std::string::npos)
    23  
    24 std::cout << "first 'needle' found at: " << found << '
    ';
    25  
    26  
    27  
    28 found=str.find("needles are small",found+1,6);
    29  
    30 if (found!=std::string::npos)
    31  
    32 std::cout << "second 'needle' found at: " << found << '
    ';
    33  
    34  
    35  
    36 found=str.find("haystack");
    37  
    38 if (found!=std::string::npos)
    39  
    40 std::cout << "'haystack' also found at: " << found << '
    ';
    41  
    42  
    43  
    44 found=str.find('.');
    45  
    46 if (found!=std::string::npos)
    47  
    48 std::cout << "Period found at: " << found << '
    ';
    49  
    50  
    51  
    52 // let's replace the first needle:
    53  
    54 str.replace(str.find(str2),str2.length(),"preposition"); //replace 用法
    55  
    56 std::cout << str << '
    ';
    57  
    58  
    59  
    60 return 0;
    61  
    62 }

    结果:
    first 'needle' found at: 14
    second 'needle' found at: 44
    'haystack' also found at: 30
    Period found at: 51
    There are two prepositions in this haystack with needles
     
     

     

    int find_first_of(char c, int pos = 0) const;//从pos开始查找字符c第一次出现的位置
    int find_first_of(const char *s, int pos = 0) const; //从pos开始查找当前串中第一个在s的前n个字符组成的数组里的字符的位置
    int find_first_of(const char *s, int pos, int n) const;
    int find_first_of(const string &s,int pos = 0) const;

    共同点:

    查找成功时返回所在位置,失败返回string::npos的值,string::npos一般是MAX_INT(即2^32 - 1)

    差异:

    find(): 查找字符串中第一次出现字符c、字符串s的位置;

    find_first_of(): 查找字符串中字符c、字符数组s中任意一个字符第一次出现的位置。


    类似的,还有rfind()和find_last_of(),以及find_first_not_of(), find_last_not_of().

    诸位正值青春年少,一定恣情放纵,贪恋香艳梅施之情,喜欢风流雅韵之事,洒脱木拘。然而诸位可知,草上露一碰即落,竹上霜一触即溶,此种风情难于长久。
  • 相关阅读:
    值得收藏的14款响应式前端开发框架
    简单几步把LOGO变字体
    7 款免费的 Metro UI 模板
    JPG渐进 & PNG/PNG24 交错测试
    你的钱,以后是放银行还是放支付宝?
    Bise IE6 在你的网站上加上它让IE滚蛋吧
    单例模式常见场景
    10 个最新的开发者工具
    大流量网站的底层系统架构
    DNS解析全过程及原理
  • 原文地址:https://www.cnblogs.com/shilipojianshen/p/13279455.html
Copyright © 2011-2022 走看看