C++ 字符串的插入、删除、替换、查找大集合(已经整理)

 

 1、字符串的插入、删除、查找

字符串的插入和删除操作一定要会,很多情况可以给你解决很多问题!!!

#include     //万能头文件
using namespace  std;

int main()
{

    cout<<"-------string insert()-----------:"<(-1);


     std::string str4 ("There are two needles in this haystack with needles.");
     std::string str5 ("needle");

     // different member versions of find in the same order as above:
     std::size_t found = str4.find(str5);
     if (found!=std::string::npos)
       cout << "first 'needle' found at: " << found << '\n';    //输出14

     found=str4.find("needles are small",found+1,6);
   //在str中的第(found+1)位开始搜索,搜索"needles are small"字符串中的前6位,找到索引位置。
     if (found!=std::string::npos)
       cout << "second 'needle' found at: " << found << '\n';   //输出44

     found=str4.find("haystack");
     if (found!=std::string::npos)
       cout << "'haystack' also found at: " << found << '\n';   //输出30

     found=str4.find('.');
     if (found!=std::string::npos)
       cout << "Period found at: " << found << '\n';            //输出51

     // let's replace the first needle:
     str4.replace(str4.find(str5),str5.length(),"preposition");  //replace 用法
     cout << str4 << '\n';


    //     find_first_of(), find_last_of(),
    //find_first_not_of(), find_last_not_of()

     string temp6 = "cag sdme";
     string temp7 = "na me";

     size_t where = temp6.find_first_of(temp7);     //也可以直接写  temp1.find_first_of("name");
     cout<<"find_first_of: "<

 

预留空位,整理子串相关!!!

你可能感兴趣的:(c++,C++学习,c++,字符串,插入,删除)