1、字符串的插入、删除、查找
字符串的插入和删除操作一定要会,很多情况可以给你解决很多问题!!!
#include <bits/stdc++.h> //万能头文件
using namespace std;
int main()
{
cout<<"-------string insert()-----------:"<<endl;
string str1="We can insert a string";
string str2="a str into ";
//在字符串指定位置index=14前面插入指定字符串
cout <<str1.insert(14,str2)<<endl;
//We can insert a str into a string
//在字符串指定位置前面插入指定字符串的子串(从指定索引开始的指定个数的字符) //2后面的9个数字
cout <<str1.insert(14,str2,2,9)<<endl;
//We can insert str into a str into a string
//插入指定字符串的前n个字符 //插入了5个元素
cout <<str1.insert(14,"test hello",5)<<endl;
//We can insert test str into a str into a string
//插入n个同样字符到字符串中
cout <<str1.insert(14,6,'*')<<endl;
//We can insert ******test str into a str into a string
cout<<"-------string replace()-----------:"<<endl;
//替换指定索引开始的指定长度的子串
cout <<str1.replace(3,3,"may")<<endl;
//We may insert ******test str into a str into a string
//用给定字符串的指定子串来进行替换
//例如以下。实际上使用的是could来进行替换
cout <<str1.replace(3,3,"can could",4,5)<<endl;
//We could insert ******test str into a str into a string
//使用给定字符串的前n个字符来进行替换:can
cout <<str1.replace(3,5,"can could",3)<<endl;
//We can insert ******test str into a str into a string
//使用指定个数的反复字符来进行替换
cout <<str1.replace(3,3,5,'*')<<endl;
//We ***** insert ******test str into a str into a string
string word="We";
size_t index=str1.find(word);
if(index!=string::npos)
//删除指定索引开始的指定长度的字符
cout <<str1.erase(index,word.length())<<endl;
// ***** insert ******test str into a str into a string
cout<<"-------string erase()-----------:"<<endl;
string str3("1234567890");
cout<<str3<<endl;
str3.erase(2,3); //把脚标为2开始的字符,连同其之后的3个字符全部删除
cout<<str3<<endl; //1267890
str3.erase(2); //把脚标为2及其之后的所有字符全部删除
cout<<str3<<endl; //12
str3.erase(); //清空所有字符
cout<<str3<<endl;
cout<<"-------string find()-----------:"<<endl;
//关于npos,找不到都会返回这个
// Value returned by various member functions when they fail.
//static const size_type npos = static_cast<size_type>(-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: "<<where<<endl; //输出索引 1 这里是temp6的索引 1 temp7中的字母第一次在temp6出现的相同字母为 a
size_t where2 = temp6.find_last_of(temp7);
cout<<"find_first_of: "<<where2<<endl; //输出索引 7 这里是temp6的索引 7 temp7中的字母最后一次在temp6出现的相同字母为 e
size_t where3 = temp6.find_last_not_of(temp7);
cout<<"find_last_not_of: "<<where3<<endl; //输出索引 5 这里是temp6的索引 5 temp7中的字母最后一次在temp6中的不同是 d
size_t where4 = temp6.find_first_not_of(temp7);
cout<<"find_first_not_of: "<<where4<<endl; //输出索引 0 这里是temp6的索引 0 temp7中的字母第一次在temp6中的不同是 c
return 0;
}
预留空位,整理子串相关!!!