总结一下string函数中的一些常用用法
2017-03-29 15:12
302 查看
参考博客 感谢!
http://blog.csdn.net/saya_/article/details/47378239
下面讲述的都是一些常用的函数:
插入 insert 查找 find 删除 erase 取子串 substr 替换 replace
1.插入 insert
str.insert(6," ") 在第6个位置插入一个字符串(在这里字符串我给省略了)
str.insert(6,str3,3,4) 在 str串中的第6个位置插入str3的串 从3位置开始 往后4个字符
例子:
find 是找到串中第一个和要查找的字符串(也可以是单个字符) 并返回其第一个字符的下标
rfind 是找到串中最后一个和要查找的字符串(也可以是单个字符) 并返回其第一个字符的下标
3.删除 erase
erase(1,10) 从1开始 往后删除字符串 长度为10
4.substr
s.substr(3,5) 从3开始取长度为5的字串
5.替换 repalce
str.replace(7, 5, "left");//按地址从&str[7]到&str[12]的内容替换为left
replace(myvector.begin(), myvector.end(), 20, 99);
http://blog.csdn.net/saya_/article/details/47378239
下面讲述的都是一些常用的函数:
插入 insert 查找 find 删除 erase 取子串 substr 替换 replace
1.插入 insert
str.insert(6," ") 在第6个位置插入一个字符串(在这里字符串我给省略了)
str.insert(6,str3,3,4) 在 str串中的第6个位置插入str3的串 从3位置开始 往后4个字符
例子:
#include <iostream> #include <string> int main () { std::string str="to be question"; std::string str2="the "; std::string str3="or not to be"; std::string::iterator it; // used in the same order as described above: str.insert(6,str2); // to be (the )question在str的第6个位置插入str2 str.insert(6,str3,3,4); // to be (not )the question在str的第6个位置插入str3的一个片段,为下标为3开始往后的4个字符 str.insert(10,"that is cool",8); // to be not (that is )the question str.insert(10,"to be "); // to be not (to be )that is the question str.insert(15,3,':'); // to be not to be(:::) that is the question std::cout << str << '\n'; return 0; }2.查找 find 和rfind 这里只说这两个
find 是找到串中第一个和要查找的字符串(也可以是单个字符) 并返回其第一个字符的下标
rfind 是找到串中最后一个和要查找的字符串(也可以是单个字符) 并返回其第一个字符的下标
#include<iostream> #include<string.h> #include<string> using namespace std; int main() { string str1="456456"; string str2="456"; string str3="654"; int a=str1.rfind(str2); int b=str1.rfind(str3); cout<<a<<endl;//输出3 cout<<b<<endl;//输出-1 return 0; }
#include<iostream> #include<string> #include<algorithm>//find的头文件 using namespace std; int main() { string str1 = "123456"; //第一个是string中找string string str2 = "34";//能找到的 string str3 = "364";//找不到的 int a = str1.find(str2); int b = str1.find(str3); cout << a << endl; cout << b << endl; /* 输出是: 2 -1 */ //第二个是string中找char char ch1 = '3';//能找到的 char ch2 = '9';//找不到的 int aa = str1.find(ch1); int bb = str1.find(ch2); cout << aa << endl; cout << bb << endl; /* 输出是: 2 -1 */ return 0; }
3.删除 erase
erase(1,10) 从1开始 往后删除字符串 长度为10
// string::erase #include <iostream> #include <string> int main() { std::string str("This is an example sentence."); std::cout << str << '\n'; // "This is an example sentence." str.erase(10, 8); std::cout << str << '\n'; // "This is an sentence." std::cout << str << '\n'; // "This is a sentence." str.erase(str.begin() + 5, str.end() - 9); std::cout << str << '\n'; // "This sentence." return 0; }
4.substr
s.substr(3,5) 从3开始取长度为5的字串
// string::substr #include <iostream> #include <string> int main () { std::string str="We think in generalities, but we live in details."; std::string str2 = str.substr (3,5); // "think" std::size_t pos = str.find("live"); // position of "live" in str std::string str3 = str.substr (pos); // get from "live" to the end std::cout << str2 << ' ' << str3 << '\n';//输出:think live in details. return 0; }
5.替换 repalce
str.replace(7, 5, "left");//按地址从&str[7]到&str[12]的内容替换为left
replace(myvector.begin(), myvector.end(), 20, 99);
相关文章推荐
- 绘图的一些常用API及基本用法总结
- numpy中一些常用函数的用法总结
- 总结了一些常用的比较细节CSS和HTML代码的用法
- 控件、UI 半年学习总结下自定义控件用法和一些常用页面效果
- 总结一下我工作中常用到的一些linux命令
- numpy中一些常用函数的用法总结
- 总结一下项目中常用的正则validate及键盘的一些事件
- 最近公司用到了lombok,感觉很不错的样子,所以上网搜了一些资料,总结了一下用法。
- [19]_总结一下find_grep_file_which\whereis_gzip\tar等常用命令的用法
- twig一些常用的用法总结
- symphony 的twig模板引擎的一些常用的用法总结
- C#中一些字符串操作的常用用法
- C#中一些字符串操作的常用用法
- C#中一些字符串操作的常用用法
- C#中一些字符串操作的常用用法(转载)
- oracle中SQL语句的一些有效常用用法
- HP-UNIX的一些命令的用法总结
- oracle中SQL语句的一些有效常用用法
- 常用的一些技术词汇对照(自己总结)
- C#中一些字符串操作的常用用法