C++基础---string类的find/find_first_of/find_first_not_of/rfind/find_last_of/find_last_not_of

1. string类的find/find_first_of/find_first_not_of/rfind/find_last_of/find_last_not_of

1.1 std::string::find

  • 原型:size_t find (const string& str, size_t pos = 0) const noexcept;
  • 说明:从源字符串起始位置pos(默认为0)处,查找有目标字符串str(string型字符串)的位置。
  • 原型:size_t find (const char* s, size_t pos = 0) const;
  • 说明:从源字符串起始位置pos(默认为0)处,查找有目标字符串s(char型字符串)的位置。
  • 原型:size_t find (const char* s, size_t pos, size_type n) const;
  • 说明:从源字符串起始位置pos处,查找有目标字符串s前n个字符(char型字符串)的位置。
  • 原型:size_t find (char c, size_t pos = 0) const noexcept;
  • 说明:从源字符串起始位置pos(默认为0)处,查找有目标字符c的位置。
  • 注意:如果找到,则返回首次匹配的开始位置,如果没有找到匹配的内容,则返回string::npos(从字符串的前端开始查找)
  • 代码示例:

    
    #include <iostream>
    
    
    #include <string>
    
    using namespace std;
    int main ()
    {
        string str1("one two three four one two three four");
    
        cout<<"size_t find (const string& str, size_t pos = npos) const noexcept;"<<endl;
        string str2("one");
        size_t found1 = str1.find(str2);
        if (found1 != string::npos)
        {
            str1.replace(found1, str2.size(), "ONE");
        }
        cout<<str1<<endl;
        cout<<endl;
    
        cout<<"size_t find (const char* s, size_t pos = npos) const;"<<endl;
        size_t found2 = str1.find("two");
        if (found2 != string::npos)
        {
            str1.replace(found2, strlen("two"), "TWO");
        }
        cout<<str1<<endl;
        cout<<endl;
    
        cout<<"size_t find (const char* s, size_t pos, size_t n) const;"<<endl;
        size_t found3 = str1.find("three", 0, 5);
        if (found3 != string::npos)
        {
            str1.replace(found3, strlen("three"), "THREE");
        }
        cout<<str1<<endl;
        cout<<endl;
    
        cout<<"size_t find (char c, size_t pos = npos) const noexcept;"<<endl;
        size_t found4 = str1.find('f');
        if (found4 != string::npos)
        {
            str1.replace(found4, sizeof('f'), "F");
        }
        cout<<str1<<endl;
        cout<<endl;
    
        system("pause");
        return 0;
    }
    =>size_t find (const string& str, size_t pos = npos) const noexcept;
      ONE two three four one two three four
    
      size_t find (const char* s, size_t pos = npos) const;
      ONE TWO three four one two three four
    
      size_t find (const char* s, size_t pos, size_t n) const;
      ONE TWO THREE four one two three four
    
      size_t find (char c, size_t pos = npos) const noexcept;
      ONE TWO THREE Four one two three four

1.2 std::string::find_first_of

  • 原型:size_t find_first_of (const string& str, size_t pos = 0) const noexcept;
  • 说明:从源字符串起始位置pos(默认为0)处,依此查找每个字符。如果某字符在目标字符串str(string型字符串)中,则返回首次匹配的该字符的位置。
  • 原型:size_t find_first_of (const char* s, size_t pos = 0) const;
  • 说明:从源字符串起始位置pos(默认为0)处,依此查找每个字符。如果某字符在目标字符串s(char型字符串)中,则返回首次匹配的该字符的位置。
  • 原型:size_t find_first_of (const char* s, size_t pos, size_t n) const;
  • 说明:从源字符串起始位置pos处,依此查找每个字符。如果某字符在目标字符串s前n个字符(char型字符串)中,则返回首次匹配的该字符的位置。
  • 原型:size_t find_first_of (char c, size_t pos = 0) const noexcept;
  • 说明:从源字符串起始位置pos(默认为0)处,依此查找每个字符。如果某字符等于目标字符c,则返回首次匹配的该字符的位置。
  • 注意:如果没有找到匹配的内容,则返回string::npos(从字符串的前端开始查找)
  • 代码示例:

    (1)代码示例一:
    
    #include <iostream>
    
    
    #include <string>
    
    using namespace std;
    int main ()
    {
        string str1("abcd efgh ijkl mnop qrst uvwx yz");
    
        cout<<"size_t find_first_of (const string& str, size_t pos = 0) const;"<<endl;
        string str2("aeimqu");
        size_t found1 = str1.find_first_of(str2);
        while (found1 != string::npos)
        {
            str1[found1]='*';
            found1 = str1.find_first_of(str2, found1+1);
        }
        cout<<str1<<endl;
        cout<<endl;
    
        cout<<"size_t find_first_of (const char* s, size_t pos = 0) const;"<<endl;
        size_t found2 = str1.find_first_of("dhlptxz");
        while (found2 != string::npos)
        {
            str1[found2]='-';
            found2 = str1.find_first_of("dhlptx", found2+1);
        }
        cout<<str1<<endl;
        cout<<endl;
    
        cout<<"size_t find_first_of (const char* s, size_t pos, size_t n) const;"<<endl;
        size_t found3 = str1.find_first_of("yz", 0, 1);
        while (found3 != string::npos)
        {
            str1[found3]='Y';
            found3 = str1.find_first_of("yz", found3+1, 1);
        }
        cout<<str1<<endl;
        cout<<endl;
    
        cout<<"size_t find_first_of (char c, size_t pos = 0) const;"<<endl;
        size_t found4 = str1.find_first_of('z');
        while (found4 != string::npos)
        {
            str1[found4]='2';
            found4 = str1.find_first_of("z", found4+1);
        }
        cout<<str1<<endl;
        cout<<endl;
    
        system("pause");
        return 0;
    }
    =>size_t find_first_of (const string& str, size_t pos = 0) const;
      *bcd *fgh *jkl *nop *rst *vwx yz
    
      size_t find_first_of (const char* s, size_t pos = 0) const;
      *bc- *fg- *jk- *no- *rs- *vw- yz
    
      size_t find_first_of (const char* s, size_t pos, size_t n) const;
      *bc- *fg- *jk- *no- *rs- *vw- Yz
    
      size_t find_first_of (char c, size_t pos = 0) const;
      *bc- *fg- *jk- *no- *rs- *vw- Y2
    (2)代码示例二:
    
    #include <iostream>
    
    
    #include <string>
    
    using namespace std;
    int main ()
    {
        string str1("abcd efgh ijkl mnop qrst uvwx yz zy xwvu tsrq ponm lkji hgfe dcba");
    
        cout<<"size_t find_first_of (const string& str, size_t pos = 0) const;"<<endl;
        string str2("aeimqu");
        size_t found1 = str1.find_first_of(str2);
        if (found1 != string::npos)
        {
            str1[found1]='*';
            //found1 = str1.find_first_of(str2, found1+1);
        }
        cout<<str1<<endl;
        cout<<endl;
    
        cout<<"size_t find_first_of (const char* s, size_t pos = 0) const;"<<endl;
        size_t found2 = str1.find_first_of("dhlptxz");
        if (found2 != string::npos)
        {
            str1[found2]='-';
            //found2 = str1.find_first_of("dhlptx", found2+1);
        }
        cout<<str1<<endl;
        cout<<endl;
    
        cout<<"size_t find_first_of (const char* s, size_t pos, size_t n) const;"<<endl;
        size_t found3 = str1.find_first_of("yz", 0, 1);
        if (found3 != string::npos)
        {
            str1[found3]='Y';
            //found3 = str1.find_first_of("yz", found3+1, 1);
        }
        cout<<str1<<endl;
        cout<<endl;
    
        cout<<"size_t find_first_of (char c, size_t pos = 0) const;"<<endl;
        size_t found4 = str1.find_first_of('z');
        if (found4 != string::npos)
        {
            str1[found4]='2';
            //found4 = str1.find_first_of("z", found4+1);
        }
        cout<<str1<<endl;
        cout<<endl;
    
        system("pause");
        return 0;
    }
    =>size_t find_first_of (const string& str, size_t pos = 0) const;
      *bcd efgh ijkl mnop qrst uvwx yz zy xwvu tsrq ponm lkji hgfe dcba
    
      size_t find_first_of (const char* s, size_t pos = 0) const;
      *bc- efgh ijkl mnop qrst uvwx yz zy xwvu tsrq ponm lkji hgfe dcba
    
      size_t find_first_of (const char* s, size_t pos, size_t n) const;
      *bc- efgh ijkl mnop qrst uvwx Yz zy xwvu tsrq ponm lkji hgfe dcba
    
      size_t find_first_of (char c, size_t pos = 0) const;
      *bc- efgh ijkl mnop qrst uvwx Y2 zy xwvu tsrq ponm lkji hgfe dcba

1.3 std::string::find_first_not_of

  • 原型:size_t find_first_not_of (const string& str, size_t pos = 0) const noexcept;
  • 说明:从源字符串起始位置pos(默认为0)处,依此查找每个字符。如果某字符不在目标字符串str(string型字符串)中,则返回首次不匹配的该字符的位置。
  • 原型:size_t find_first_not_of (const char* s, size_t pos = 0) const;
  • 说明:从源字符串起始位置pos(默认为0)处,依此查找每个字符。如果某字符不在目标字符串s(char型字符串)中,则返回首次不匹配的该字符的位置。
  • 原型:size_t find_first_not_of (const char* s, size_t pos, size_t n) const;
  • 说明:从源字符串起始位置pos处,依此查找每个字符。如果某字符不在目标字符串s前n个字符(char型字符串)中,则返回首次不匹配的该字符的位置。
  • 原型:size_t find_first_not_of (char c, size_t pos = 0) const noexcept;
  • 说明:从源字符串起始位置pos(默认为0)处,依此查找每个字符。如果某字符不等于目标字符c,则返回首次不匹配的该字符的位置。
  • 注意:如果没有找到不匹配的内容,则返回string::npos(从字符串的前端开始查找)
  • 代码示例:

    (1)示例代码一:
    
    #include <iostream>
    
    
    #include <string>
    
    using namespace std;
    int main ()
    {
        string str1("abcd efgh ijkl mnop qrst uvwx yz");
    
        cout<<"size_t find_first_not_of (const string& str, size_t pos = 0) const;"<<endl;
        string str2("bcd fgh jkl nop rst vwx yz");
        size_t found1 = str1.find_first_not_of(str2);
        while (found1 != string::npos)
        {
            str1[found1]='*';
            found1 = str1.find_first_not_of(str2, found1+1);
        }
        cout<<str1<<endl;
        cout<<endl;
    
        cout<<"size_t find_first_not_of (const char* s, size_t pos = 0) const;"<<endl;
        size_t found2 = str1.find_first_not_of("abc efg ijk mno qrs uvw yz *");
        while (found2 != string::npos)
        {
            str1[found2]='-';
            found2 = str1.find_first_not_of("abc efg ijk mno qrs uvw yz *", found2+1);
        }
        cout<<str1<<endl;
        cout<<endl;
    
        cout<<"size_t find_first_not_of (const char* s, size_t pos, size_t n) const;"<<endl;
        size_t found3 = str1.find_first_not_of("abcd efgh ijkl mnop qrst uvwx z * -", 0, strlen("abcd efgh ijkl mnop qrst uvwx z * -"));
        while (found3 != string::npos)
        {
            str1[found3]='Y';
            found3 = str1.find_first_not_of("abcd efgh ijkl mnop qrst uvwx z * -", found3+1, strlen("abcd efgh ijkl mnop qrst uvwx z * -"));
        }
        cout<<str1<<endl;
        cout<<endl;
    
        cout<<"size_t find_first_not_of (char c, size_t pos = 0) const;"<<endl;
        size_t found4 = str1.find_first_not_of('z', str1.size()-1);
        if (found4 == string::npos)
        {
            str1[str1.size()-1]='2';
        }
        cout<<str1<<endl;
        cout<<endl;
    
        system("pause");
        return 0;
    }
    =>size_t find_first_of (const string& str, size_t pos = 0) const;
      *bcd *fgh *jkl *nop *rst *vwx yz
    
      size_t find_first_of (const char* s, size_t pos = 0) const;
      *bc- *fg- *jk- *no- *rs- *vw- yz
    
      size_t find_first_of (const char* s, size_t pos, size_t n) const;
      *bc- *fg- *jk- *no- *rs- *vw- Yz
    
      size_t find_first_of (char c, size_t pos = 0) const;
      *bc- *fg- *jk- *no- *rs- *vw- Y2
    (2)示例代码二:
    
    #include <iostream>
    
    
    #include <string>
    
    using namespace std;
    int main ()
    {
        string str1("abcd efgh ijkl mnop qrst uvwx yz zy xwvu tsrq ponm lkji hgfe dcba");
    
        cout<<"size_t find_first_not_of (const string& str, size_t pos = 0) const;"<<endl;
        string str2("bcd fgh jkl nop rst vwx yz");
        size_t found1 = str1.find_first_not_of(str2);
        if (found1 != string::npos)
        {
            str1[found1]='*';
            //found1 = str1.find_first_not_of(str2, found1+1);
        }
        cout<<str1<<endl;
        cout<<endl;
    
        cout<<"size_t find_first_not_of (const char* s, size_t pos = 0) const;"<<endl;
        size_t found2 = str1.find_first_not_of("abc efg ijk mno qrs uvw yz *");
        if (found2 != string::npos)
        {
            str1[found2]='-';
            //found2 = str1.find_first_not_of("abc efg ijk mno qrs uvw yz *", found2+1);
        }
        cout<<str1<<endl;
        cout<<endl;
    
        cout<<"size_t find_first_not_of (const char* s, size_t pos, size_t n) const;"<<endl;
        size_t found3 = str1.find_first_not_of("abcd efgh ijkl mnop qrst uvwx z * -", 0, strlen("abcd efgh ijkl mnop qrst uvwx z * -"));
        if (found3 != string::npos)
        {
            str1[found3]='Y';
            //found3 = str1.find_first_not_of("abcd efgh ijkl mnop qrst uvwx z * -", found3+1, strlen("abcd efgh ijkl mnop qrst uvwx z * -"));
        }
        cout<<str1<<endl;
        cout<<endl;
    
        cout<<"size_t find_first_not_of (char c, size_t pos = 0) const;"<<endl;
        size_t found4 = str1.find_first_not_of('a', str1.size()-1);
        if (found4 == string::npos)
        {
            str1[str1.size()-1]='A';
        }
        cout<<str1<<endl;
        cout<<endl;
    
        system("pause");
        return 0;
    }
    =>size_t find_first_not_of (const string& str, size_t pos = 0) const;
      *bcd efgh ijkl mnop qrst uvwx yz zy xwvu tsrq ponm lkji hgfe dcba
    
      size_t find_first_not_of (const char* s, size_t pos = 0) const;
      *bc- efgh ijkl mnop qrst uvwx yz zy xwvu tsrq ponm lkji hgfe dcba
    
      size_t find_first_not_of (const char* s, size_t pos, size_t n) const;
      *bc- efgh ijkl mnop qrst uvwx Yz zy xwvu tsrq ponm lkji hgfe dcba
    
      size_t find_first_not_of (char c, size_t pos = 0) const;
      *bc- efgh ijkl mnop qrst uvwx Yz zy xwvu tsrq ponm lkji hgfe dcbA

1.4 std::string::rfind

  • 原型:size_t rfind (const string& str, size_t pos = npos) const noexcept;
  • 说明:从源字符串起始位置pos(默认为npos)处,查找有目标字符串str(string型字符串)的位置。
  • 原型:size_t rfind (const char* s, size_t pos = npos) const;
  • 说明:从源字符串起始位置pos(默认为npos)处,查找有目标字符串s(char型字符串)的位置。
  • 原型:size_t rfind (const char* s, size_t pos, size_t n) const;
  • 说明:从源字符串起始位置pos处,查找有目标字符串s前n个字符(char型字符串)的位置。
  • 原型:size_t rfind (char c, size_t pos = npos) const noexcept;
  • 说明:从源字符串起始位置pos(默认为npos)处,查找有目标字符c的位置。
  • 注意:如果找到,则返回首次匹配的开始位置,如果没有找到匹配的内容,则返回string::npos(从字符串的末端开始查找)
  • 代码示例:

    
    #include <iostream>
    
    
    #include <string>
    
    using namespace std;
    int main ()
    {
        string str1("one two three four one two three four");
    
        cout<<"size_t rfind (const string& str, size_t pos = npos) const noexcept;"<<endl;
        string str2("one");
        size_t found1 = str1.rfind(str2);
        if (found1 != string::npos)
        {
            str1.replace(found1, str2.size(), "ONE");
        }
        cout<<str1<<endl;
        cout<<endl;
    
        cout<<"size_t rfind (const char* s, size_t pos = npos) const;"<<endl;
        size_t found2 = str1.rfind("two");
        if (found2 != string::npos)
        {
            str1.replace(found2, strlen("two"), "TWO");
        }
        cout<<str1<<endl;
        cout<<endl;
    
        cout<<"size_t rfind (const char* s, size_t pos, size_t n) const;"<<endl;
        size_t found3 = str1.rfind("three", string::npos, 5);
        if (found3 != string::npos)
        {
            str1.replace(found3, strlen("three"), "THREE");
        }
        cout<<str1<<endl;
        cout<<endl;
    
        cout<<"size_t rfind (char c, size_t pos = npos) const noexcept;"<<endl;
        size_t found4 = str1.rfind('f');
        if (found4 != string::npos)
        {
            str1.replace(found4, sizeof('f'), "F");
        }
        cout<<str1<<endl;
        cout<<endl;
    
        system("pause");
        return 0;
    }
    =>size_t rfind (const string& str, size_t pos = npos) const noexcept;
      one two three four ONE two three four
    
      size_t rfind (const char* s, size_t pos = npos) const;
      one two three four ONE TWO three four
    
      size_t rfind (const char* s, size_t pos, size_t n) const;
      one two three four ONE TWO THREE four
    
      size_t rfind (char c, size_t pos = npos) const noexcept;
      one two three four ONE TWO THREE Four

1.5 std::string::size_t find_last_of

  • 原型:size_t find_last_of (const string& str, size_t pos = npos) const noexcept;
  • 说明:从源字符串起始位置pos(默认为npos)处,依此查找每个字符。如果某字符在目标字符串str(string型字符串)中,则返回首次匹配的该字符的位置。
  • 原型:size_t find_last_of (const char* s, size_t pos = npos) const;
  • 说明:从源字符串起始位置pos(默认为npos)处,依此查找每个字符。如果某字符在目标字符串s(char型字符串)中,则返回首次匹配的该字符的位置。
  • 原型:size_t find_last_of (const char* s, size_t pos, size_t n) const;
  • 说明:从源字符串起始位置pos处,依此查找每个字符。如果某字符在目标字符串s前n个字符(char型字符串)中,则返回首次匹配的该字符的位置。
  • 原型:size_t find_last_of (char c, size_t pos = npos) const noexcept;
  • 说明:从源字符串起始位置pos(默认为npos)处,依此查找每个字符。如果某字符等于目标字符c,则返回首次匹配的该字符的位置。
  • 注意:如果没有找到匹配的内容,则返回string::npos(从字符串的末端开始查找)
  • 代码示例:

    (1)代码示例一:
    
    #include <iostream>
    
    
    #include <string>
    
    using namespace std;
    int main ()
    {
        string str1("abcd efgh ijkl mnop qrst uvwx yz");
    
        cout<<"size_t find_last_of (const string& str, size_t pos = npos) const;"<<endl;
        string str2("aeimqu");
        size_t found1 = str1.find_last_of(str2);
        while (found1 != string::npos)
        {
            str1[found1]='*';
            found1 = str1.find_last_of(str2, found1-1);
        }
        cout<<str1<<endl;
        cout<<endl;
    
        cout<<"size_t find_last_of (const char* s, size_t pos = npos) const;"<<endl;
        size_t found2 = str1.find_last_of("dhlptx");
        while (found2 != string::npos)
        {
            str1[found2]='-';
            found2 = str1.find_last_of("dhlptx", found2-1);
        }
        cout<<str1<<endl;
        cout<<endl;
    
        cout<<"size_t find_last_of (const char* s, size_t pos, size_t n) const;"<<endl;
        size_t found3 = str1.find_last_of("y", string::npos, 1);
        while (found3 != string::npos)
        {
            str1[found3]='Y';
            found3 = str1.find_last_of("y", found3-1, 1);
        }
        cout<<str1<<endl;
        cout<<endl;
    
        cout<<"size_t find_last_of (char c, size_t pos = npos) const;"<<endl;
        size_t found4 = str1.find_last_of('z');
        while (found4 != string::npos)
        {
            str1[found4]='2';
            found4 = str1.find_last_of("z", found4-1);
        }
        cout<<str1<<endl;
        cout<<endl;
    
        system("pause");
        return 0;
    }
    =>size_t find_last_of (const string& str, size_t pos = npos) const;
      *bcd *fgh *jkl *nop *rst *vwx yz
    
      size_t find_last_of (const char* s, size_t pos = npos) const;
      *bc- *fg- *jk- *no- *rs- *vw- yz
    
      size_t find_last_of (const char* s, size_t pos, size_t n) const;
      *bc- *fg- *jk- *no- *rs- *vw- Yz
    
      size_t find_last_of (char c, size_t pos = npos) const;
      *bc- *fg- *jk- *no- *rs- *vw- Y2
    (2)代码示例二:
    
    #include <iostream>
    
    
    #include <string>
    
    using namespace std;
    int main ()
    {
        string str1("abcd efgh ijkl mnop qrst uvwx yz zy xwvu tsrq ponm lkji hgfe dcba");
    
        cout<<"size_t find_last_of (const string& str, size_t pos = string::npos) const;"<<endl;
        string str2("aeimqu");
        size_t found1 = str1.find_last_of(str2);
        if (found1 != string::npos)
        {
            str1[found1]='*';
            //found1 = str1.find_last_of(str2, found1-1);
        }
        cout<<str1<<endl;
        cout<<endl;
    
        cout<<"size_t find_last_of (const char* s, size_t pos = string::npos) const;"<<endl;
        size_t found2 = str1.find_last_of("dhlptxz");
        if (found2 != string::npos)
        {
            str1[found2]='-';
            //found2 = str1.find_last_of("dhlptx", found2-1);
        }
        cout<<str1<<endl;
        cout<<endl;
    
        cout<<"size_t find_last_of (const char* s, size_t pos, size_t n) const;"<<endl;
        size_t found3 = str1.find_last_of("yz", string::npos, 1);
        if (found3 != string::npos)
        {
            str1[found3]='Y';
            //found3 = str1.find_last_of("yz", found3-1, 1);
        }
        cout<<str1<<endl;
        cout<<endl;
    
        cout<<"size_t find_last_of (char c, size_t pos = string::npos) const;"<<endl;
        size_t found4 = str1.find_last_of('z');
        if (found4 != string::npos)
        {
            str1[found4]='2';
            //found4 = str1.find_last_of("z", found4-1);
        }
        cout<<str1<<endl;
        cout<<endl;
    
        system("pause");
        return 0;
    }
    =>size_t find_last_of (const string& str, size_t pos = string::npos) const;
      abcd efgh ijkl mnop qrst uvwx yz zy xwvu tsrq ponm lkji hgfe dcb*
    
      size_t find_last_of (const char* s, size_t pos = string::npos) const;
      abcd efgh ijkl mnop qrst uvwx yz zy xwvu tsrq ponm lkji hgfe -cb*
    
      size_t find_last_of (const char* s, size_t pos, size_t n) const;
      abcd efgh ijkl mnop qrst uvwx yz zY xwvu tsrq ponm lkji hgfe -cb*
    
      size_t find_last_of (char c, size_t pos = string::npos) const;
      abcd efgh ijkl mnop qrst uvwx yz 2Y xwvu tsrq ponm lkji hgfe -cb*

1.6 std::string::find_last_not_of

  • 原型:size_t find_last_not_of (const string& str, size_t pos = npos) const noexcept;
  • 说明:从源字符串起始位置pos(默认为npos)处,依此查找每个字符。如果某字符不在目标字符串str(string型字符串)中,则返回首次不匹配的该字符的位置。
  • 原型:size_t find_last_not_of (const char* s, size_t pos = npos) const;
  • 说明:从源字符串起始位置pos(默认为npos)处,依此查找每个字符。如果某字符不在目标字符串s(char型字符串)中,则返回首次不匹配的该字符的位置。
  • 原型:size_t find_first_not_of (const char* s, size_t pos, size_t n) const;
  • 说明:从源字符串起始位置pos处,依此查找每个字符。如果某字符不在目标字符串s前n个字符(char型字符串)中,则返回首次不匹配的该字符的位置。
  • 原型:size_t find_last_not_of (const char* s, size_t pos, size_t n) const;
  • 说明:从源字符串起始位置pos(默认为npos)处,依此查找每个字符。如果某字符不等于目标字符c,则返回首次不匹配的该字符的位置。
  • 注意:如果没有找到不匹配的内容,则返回string::npos(从字符串的末端开始查找)
  • 代码示例:

    (1)示例代码一:
    
    #include <iostream>
    
    
    #include <string>
    
    using namespace std;
    int main ()
    {
        string str1("abcd efgh ijkl mnop qrst uvwx yz");
    
        cout<<"size_t find_last_not_of (const string& str, size_t pos = npos) const;"<<endl;
        string str2("bcd fgh jkl nop rst vwx yz *");
        size_t found1 = str1.find_last_not_of(str2);
        while (found1 != string::npos)
        {
            str1[found1]='*';
            found1 = str1.find_last_not_of(str2, found1-1);
        }
        cout<<str1<<endl;
        cout<<endl;
    
        cout<<"size_t find_last_not_of (const char* s, size_t pos = npos) const;"<<endl;
        size_t found2 = str1.find_last_not_of("abc efg ijk mno qrs uvw yz * -");
        while (found2 != string::npos)
        {
            str1[found2]='-';
            found2 = str1.find_last_not_of("abc efg ijk mno qrs uvw yz * -", found2-1);
        }
        cout<<str1<<endl;
        cout<<endl;
    
        cout<<"size_t find_last_not_of (const char* s, size_t pos, size_t n) const;"<<endl;
        size_t found3 = str1.find_last_not_of("abcd efgh ijkl mnop qrst uvwx z * - Y", string::npos, strlen("abcd efgh ijkl mnop qrst uvwx z * - Y"));
        while (found3 != string::npos)
        {
            str1[found3]='Y';
            found3 = str1.find_last_not_of("abcd efgh ijkl mnop qrst uvwx z * - Y", found3-1, strlen("abcd efgh ijkl mnop qrst uvwx z * - Y"));
        }
        cout<<str1<<endl;
        cout<<endl;
    
        cout<<"size_t find_last_not_of (char c, size_t pos = npos) const;"<<endl;
        size_t found4 = str1.find_last_not_of('2');
        if (found4 != string::npos)
        {
            str1[found4]='2';
            //found4 = str1.find_last_not_of('2', found4-1);
        }
        cout<<str1<<endl;
        cout<<endl;
    
        system("pause");
        return 0;
    }
    =>size_t find_last_not_of (const string& str, size_t pos = npos) const;
      *bcd *fgh *jkl *nop *rst *vwx yz
    
      size_t find_last_not_of (const char* s, size_t pos = npos) const;
      *bc- *fg- *jk- *no- *rs- *vw- yz
    
      size_t find_last_not_of (const char* s, size_t pos, size_t n) const;
      *bc- *fg- *jk- *no- *rs- *vw- Yz
    
      size_t find_last_not_of (char c, size_t pos = npos) const;
      *bc- *fg- *jk- *no- *rs- *vw- Y2
    (2)示例代码二:
    
    #include <iostream>
    
    
    #include <string>
    
    using namespace std;
    int main ()
    {
        string str1("abcd efgh ijkl mnop qrst uvwx yz zy xwvu tsrq ponm lkji hgfe dcba");
    
        cout<<"size_t find_last_not_of (const string& str, size_t pos = npos) const;"<<endl;
        string str2("bcd fgh jkl nop rst vwx yz *");
        size_t found1 = str1.find_last_not_of(str2);
        if (found1 != string::npos)
        {
            str1[found1]='*';
            //found1 = str1.find_last_not_of(str2, found1-1);
        }
        cout<<str1<<endl;
        cout<<endl;
    
        cout<<"size_t find_last_not_of (const char* s, size_t pos = npos) const;"<<endl;
        size_t found2 = str1.find_last_not_of("abc efg ijk mno qrs uvw yz * -");
        if (found2 != string::npos)
        {
            str1[found2]='-';
            //found2 = str1.find_last_not_of("abc efg ijk mno qrs uvw yz * -", found2-1);
        }
        cout<<str1<<endl;
        cout<<endl;
    
        cout<<"size_t find_last_not_of (const char* s, size_t pos, size_t n) const;"<<endl;
        size_t found3 = str1.find_last_not_of("abcd efgh ijkl mnop qrst uvwx z * - Y", string::npos, strlen("abcd efgh ijkl mnop qrst uvwx z * - Y"));
        if (found3 != string::npos)
        {
            str1[found3]='Y';
            //found3 = str1.find_last_not_of("abcd efgh ijkl mnop qrst uvwx z * - Y", found3-1, strlen("abcd efgh ijkl mnop qrst uvwx z * - Y"));
        }
        cout<<str1<<endl;
        cout<<endl;
    
        cout<<"size_t find_last_not_of (char c, size_t pos = npos) const;"<<endl;
        size_t found4 = str1.find_last_not_of('a', 0);
        if (found4 == string::npos)
        {
            str1[0]='A';
        }
        cout<<str1<<endl;
        cout<<endl;
    
        system("pause");
        return 0;
    }
    =>size_t find_last_not_of (const string& str, size_t pos = npos) const;
      abcd efgh ijkl mnop qrst uvwx yz zy xwvu tsrq ponm lkji hgfe dcb*
    
      size_t find_last_not_of (const char* s, size_t pos = npos) const;
      abcd efgh ijkl mnop qrst uvwx yz zy xwvu tsrq ponm lkji hgfe -cb*
    
      size_t find_last_not_of (const char* s, size_t pos, size_t n) const;
      abcd efgh ijkl mnop qrst uvwx yz zY xwvu tsrq ponm lkji hgfe -cb*
    
      size_t find_last_not_of (char c, size_t pos = npos) const;
      Abcd efgh ijkl mnop qrst uvwx yz zY xwvu tsrq ponm lkji hgfe -cb*

参考文献:
[1] 网络资源:
http://www.cplusplus.com/reference/string/string/find/
http://www.cplusplus.com/reference/string/string/find_first_of/
http://www.cplusplus.com/reference/string/string/find_first_not_of/
http://www.cplusplus.com/reference/string/string/rfind/
http://www.cplusplus.com/reference/string/string/find_last_of/
http://www.cplusplus.com/reference/string/string/find_last_not_of/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值