C++中对字符串进行插入、替换、删除操作
2017-06-06 10:59
495 查看
#include <iostream> #include <string> using std::cout; using std::endl; using std::string; int main(void){ string str1="We can insert a string"; string str2="a str into "; //在字符串指定位置前面插入指定字符串 cout <<str1.insert(14,str2)<<endl; //在字符串指定位置前面插入指定字符串的子串(从指定索引開始的指定个数的字符) cout <<str1.insert(14,str2,2,9)<<endl; //插入指定字符串的前n个字符 cout <<str1.insert(14,"test hello",5)<<endl; //插入n个同样字符到字符串中 cout <<str1.insert(14,6,'*')<<endl; //替换指定索引開始的指定长度的子串 cout <<str1.replace(3,3,"may")<<endl; //用给定字符串的指定子串来进行替换 //例如以下。实际上使用的是could来进行替换 cout <<str1.replace(3,3,"can could",4,5)<<endl; //使用给定字符串的前n个字符来进行替换:can cout <<str1.replace(3,5,"can could",3)<<endl; //使用指定个数的反复字符来进行替换 cout <<str1.replace(3,3,5,'*')<<endl; string word="We"; size_t index=str1.find(word); if(index!=string::npos) //删除指定索引開始的指定长度的字符 cout <<str1.erase(index,word.length())<<endl; return 0; }
相关文章推荐
- C++中对字符串进行插入、替换、删除操作
- 字符串操作——插入、替换、删除
- JS 字符串操作函数 往指定位置插入字符 删除指定位置字符 替换指定位置字符
- 每天一道LeetCode-----使用最少的操作将一个字符串转换成另一个字符串,只有插入,删除,替换三种操作
- c++字符串的插入与删除操作
- c++ stl Editor自定义类 对字符串 查找 替换 删除 插入
- JS 字符串操作函数 往指定位置插入字符 删除指定位置字符 替换指定位置字符
- JAVA对MYSQL进行连接、插入、修改、删除操作
- 用C语言操作MySQL数据库,进行连接、插入、修改、删除等操作
- Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- 转载:怎样在C#里对xml文件进行修改,新增,删除,插入操作?
- 黄聪:Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- 用C语言操作MySQL数据库,进行连接、插入、修改、删除等操作
- StrinBuilder 追加 ,插入,删除,和替换操作
- 怎样在C#里对xml文件进行修改,新增,删除,插入操作?
- Javascript splice() 对数组的操作(插入、删除、替换元素)
- 链表的相关操作查找插入删除(c++ 数据结构)
- Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- C#里对xml文件进行修改,新增,删除,插入操作
- 对单链表进行 删除、插入、排序 等操作。