C++ sting字符串函数详解
来源:互联网 发布:兔先森质量 知乎 编辑:程序博客网 时间:2024/06/06 00:40
文章转载自: http://www.renfei.org/blog/introduction-to-cpp-string.html
1. 运算符重载
a. + 和 +=: 连接字符串
b. =: 字符串赋值
c. >、>=、< 和 <=: 字符串比较(例如a < b, aa < ab)
d. ==、!=: 比较字符串
e. <<、>>: 输出、输入字符串
注意:使用重载的运算符 + 时,必须保证前两个操作数至少有一个为 string 类型。例如,下面的写法是不合法的:
#include <iostream>#include <string>int main(){ string str = "cat"; cout << "apple" + "boy" + str; // illegal! return 0;}
string str;cin >> str;str.find("ab");//返回字符串 ab 在 str 的位置str.find("ab", 2);//在 str[2]~str[n-1] 范围内查找并返回字符串 ab 在 str 的位置str.rfind("ab", 2);//在 str[0]~str[2] 范围内查找并返回字符串 ab 在 str 的位置//first 系列函数str.find_first_of("apple");//返回 apple 首次在 str 中出现的位置str.find_first_of("apple", 2);//返回 apple 首次在 str[2]~str[n-1] 范围中出现的位置str.find_first_not_of("apple");//返回除 apple 以外的任何一个字符在 str 中首次出现的位置str.find_first_not_of("apple", 2);//返回除 apple 以外的任何一个字符在 str[2]~str[n-1] 范围中首次出现的位置//last 系列函数str.find_last_of("apple");//返回 apple 最后一次在 str 中出现的位置str.find_last_of("apple", 2);//返回 apple 最后一次在 str[0]~str[2] 范围中出现的位置str.find_last_not_of("apple");//返回除 apple 以外的任何一个字符在 str 中最后一次出现的位置str.find_last_not_of("apple", 2);//返回除 apple 以外的任何一个字符在 str[0]~str[2] 范围中最后一次出现的位置//以上函数如果没有找到,均返回string::nposcout << string::npos;
str.substr(3); //返回 [3] 及以后的子串str.substr(2, 4); //返回 str[2]~str[2+(4-1)] 子串(即从[2]开始4个字符组成的字符串)
4. 替换
str.replace(2, 4, "sz");//返回把 [2]~[2+(4-1)] 的内容替换为 "sz" 后的新字符串str.replace(2, 4, "abcd", 3);//返回把 [2]~[2+(4-1)] 的内容替换为 "abcd" 的前3个字符后的新字符串
5. 插入
str.insert(2, "sz");//从 [2] 位置开始添加字符串 "sz",并返回形成的新字符串str.insert(2, "abcd", 3);//从 [2] 位置开始添加字符串 "abcd" 的前 3 个字符,并返回形成的新字符串str.insert(2, "abcd", 1, 3);//从 [2] 位置开始添加字符串 "abcd" 的前 [2]~[2+(3-1)] 个字符,并返回形成的新字符串
6. 删除
str.erase(3);//删除 [3] 及以后的字符,并返回新字符串str.erase(3, 5);//删除从 [3] 开始的 5 个字符,并返回新字符串
str1.swap(str2);//把 str1 与 str2 交换
8. 其他
str.size();//返回字符串长度str.length();//返回字符串长度str.empty();//检查 str 是否为空,为空返回 1,否则返回 0str[n];//存取 str 第 n + 1 个字符str.at(n);//存取 str 第 n + 1 个字符(如果溢出会抛出异常)
9. 实例
a. 查找给定字符串并把相应子串替换为另一给定字符串
string 并没有提供这样的函数,所以我们自己来实现。由于给定字符串可能出现多次,所以需要用到 find() 成员函数的第二个参数,每次查找之后,从找到位置往后继续搜索。直接看代码(这个函数返回替换的次数,如果返回值是 0 说明没有替换)
int str_replace(string &str, const string &src, const string &dest) //在str中把所有src替换为dest{ int counter = 0; string::size_type pos = 0; while ((pos = str.find(src, pos)) != string::npos) { str.replace(pos, src.size(), dest); ++counter; pos += dest.size(); } return counter;}
b. 从给定字符串中删除一给定字串
方法和上面相似,内部使用 erase() 完成。
int str_erase(string &str, const string src) // 在str中删除所有的src{ int counter = 0; string::size_type pos = 0; while ((pos = str.find(src, pos)) != string::npos) { str.erase(pos, src.size()); ++counter; } return counter;}
1 0
- C++ sting字符串函数详解
- c语言字符串函数详解
- C语言字符串函数详解
- c语言字符串函数详解
- c语言字符串函数详解
- c语言字符串函数详解
- C语言字符串函数详解
- c语言字符串函数详解
- c语言字符串函数详解
- C语言字符串函数详解
- c语言字符串函数详解
- c语言字符串函数详解
- C语言字符串函数详解
- C语言字符串函数详解
- c语言字符串函数详解
- C语言字符串函数详解
- C语言字符串函数详解
- c语言字符串函数详解
- Cordys 使用Oracle同义词解决方案
- 反射Reflect
- 从相册或拍照选择图片并剪切
- linux 线程编程:互斥锁创建
- qt 与MySQL
- C++ sting字符串函数详解
- Python学习第一篇之python 连接字符串和数字
- WebView和js交互
- Codevs3657括号序列题解
- 第37讲-项目四-大奖赛计分
- Flex4.6+blazeDS+MyEclipse10+SQLSERVER项目环境搭建
- UIScrollView 之 EXC_BAD_ACCESS
- Fragment介绍
- LeetCode | Reverse Integer