C++ string 字符串函数详解
运算符重载
- + 和 +=:连接字符串
- =:字符串赋值
- >、>=、< 和 <=:字符串比较(例如a < b, aa < ab)
- ==、!=:比较字符串
- <<、>>:输出、输入字符串
注意:使用重载的运算符 + 时,必须保证前两个操作数至少有一个为 string 类型。例如,下面的写法是不合法的:
#include <iostream>
#include <string>
int main()
{
string str = "cat";
cout << "apple" + "boy" + str; // illegal!
return ;
}
查找
string str;
cin >> str; str.find("ab");//返回字符串 ab 在 str 的位置
str.find("ab", );//在 str[2]~str[n-1] 范围内查找并返回字符串 ab 在 str 的位置
str.rfind("ab", );//在 str[0]~str[2] 范围内查找并返回字符串 ab 在 str 的位置 //first 系列函数
str.find_first_of("apple");//返回 apple 中任何一个字符首次在 str 中出现的位置
str.find_first_of("apple", );//返回 apple 中任何一个字符首次在 str[2]~str[n-1] 范围中出现的位置
str.find_first_not_of("apple");//返回除 apple 以外的任何一个字符在 str 中首次出现的位置
str.find_first_not_of("apple", );//返回除 apple 以外的任何一个字符在 str[2]~str[n-1] 范围中首次出现的位置 //last 系列函数
str.find_last_of("apple");//返回 apple 中任何一个字符最后一次在 str 中出现的位置
str.find_last_of("apple", );//返回 apple 中任何一个字符最后一次在 str[0]~str[2] 范围中出现的位置
str.find_last_not_of("apple");//返回除 apple 以外的任何一个字符在 str 中最后一次出现的位置
str.find_last_not_of("apple", );//返回除 apple 以外的任何一个字符在 str[0]~str[2] 范围中最后一次出现的位置 //以上函数如果没有找到,均返回string::npos
cout << string::npos;
子串
str.substr(); //返回 [3] 及以后的子串
str.substr(, ); //返回 str[2]~str[2+(4-1)] 子串(即从[2]开始4个字符组成的字符串)
替换
str.replace(, , "sz");//返回把 [2]~[2+(4-1)] 的内容替换为 "sz" 后的新字符串
str.replace(, , "abcd", );//返回把 [2]~[2+(4-1)] 的内容替换为 "abcd" 的前3个字符后的新字符串
插入
str.insert(, "sz");//从 [2] 位置开始添加字符串 "sz",并返回形成的新字符串
str.insert(, "abcd", );//从 [2] 位置开始添加字符串 "abcd" 的前 3 个字符,并返回形成的新字符串
str.insert(, "abcd", , );//从 [2] 位置开始添加字符串 "abcd" 的前 [2]~[2+(3-1)] 个字符,并返回形成的新字符串
追加
除了用重载的 +
操作符,还可以使用函数来完成。
str.push_back('a');//在 str 末尾添加字符'a'
str.append("abc");//在 str 末尾添加字符串"abc"
删除
str.erase();//删除 [3] 及以后的字符,并返回新字符串
str.erase(, );//删除从 [3] 开始的 5 个字符,并返回新字符串
交换
str1.swap(str2);//把 str1 与 str2 交换
其他
str.size();//返回字符串长度
str.length();//返回字符串长度
str.empty();//检查 str 是否为空,为空返回 1,否则返回 0
str[n];//存取 str 第 n + 1 个字符
str.at(n);//存取 str 第 n + 1 个字符(如果溢出会抛出异常)
实例
查找给定字符串并把相应子串替换为另一给定字符串
string 并没有提供这样的函数,所以我们自己来实现。由于给定字符串可能出现多次,所以需要用到 find()
成员函数的第二个参数,每次查找之后,从找到位置往后继续搜索。直接看代码(这个函数返回替换的次数,如果返回值是 0 说明没有替换):
int str_replace(string &str, const string &src, const string &dest)
{
int counter = ;
string::size_type pos = ;
while ((pos = str.find(src, pos)) != string::npos) {
str.replace(pos, src.size(), dest);
++counter;
pos += dest.size();
}
return counter;
}
从给定字符串中删除一给定字串
方法和上面相似,内部使用 erase()
完成。代码:
int str_erase(string &str, const string src)
{
int counter = ;
string::size_type pos = ;
while ((pos = str.find(src, pos)) != string::npos) {
str.erase(pos, src.size());
++counter;
}
return counter;
}
给定一字符串和一字符集,从字符串剔除字符集中的任意字符
int str_wash(string &str, const string src)
{
int counter = ;
string::size_type pos = ;
while ((pos = str.find_first_of(src, pos)) != string::npos) {
str.erase(pos, );
++counter;
}
return counter;
}
引用自:https://www.renfei.org/blog/introduction-to-cpp-string.html
C++ string 字符串函数详解的更多相关文章
- python pandas字符串函数详解(转)
pandas字符串函数详解(转)——原文连接见文章末尾 在使用pandas框架的DataFrame的过程中,如果需要处理一些字符串的特性,例如判断某列是否包含一些关键字,某列的字符长度是否小于3等等 ...
- 17.C++-string字符串类(详解)
C++字符串string类 在C语言里,字符串是用字符数组来表示的,而对于应用层而言,会经常用到字符串,而继续使用字符数组,就使得效率非常低. 所以在C++标准库里,通过类string从新自定义了字符 ...
- oracle中的字符串函数详解
花了点时间 复习.了一下字符串函数 希望对初学者有帮助 ----------连接字符串函数-----------------select concat('leiyi','hubei') from du ...
- Mysql 字符串函数 详解
字符串函数是最常用的一种函数了,如果大家编写过程序的话,不妨回过头去看看自己使用过的函数,可能会惊讶地发现字符串处理的相关函数占已使用过的函数很大一部分.MySQL中字符串函数也是最丰富的一类函数,表 ...
- c语言字符串函数详解
转载请注明来源:https://www.cnblogs.com/hookjc/ oid *memset(void *dest, int c, size_t count); 将dest前面count个字 ...
- 使用JS截取字符串函数详解
一.函数:split() 功能:使用一个指定的分隔符把一个字符串分割存储到数组 例子: str=”jpg|bmp|gif|ico|png”; arr=theString.split(”|”); //a ...
- Java String字符串深入详解
Java中字符串对象创建有两种形式,一种为字面量形式,如String str = "hello";,另一种就是使用new这种标准的构造对象的方法,如String str = new ...
- php字符串函数详解
nl2br 功能:化换行符为<br> <?php $str = "cat isn't \n dog"; $result = nl2br($str); echo $ ...
- C#的String.Split 分割字符串用法详解的代码
代码期间,把代码过程经常用的内容做个珍藏,下边代码是关于C#的String.Split 分割字符串用法详解的代码,应该对码农们有些用途. 1) public string[] Split(params ...
随机推荐
- Note | Ubuntu
目录 0. 教程 1. 安装 2. 系统 0. 教程 <Linux就该这么学>:https://www.cnblogs.com/RyanXing/p/9462850.html 1. 安装 ...
- ImportError: cannot import name 'render_to_response' 解决方法
前几天 Django 官方推出了 3.0 框架,项目在 K8S 内部署启动的时候,报了这个错:ImportError: cannot import name 'render_to_response' ...
- Entity Framework 6 中如何获取 EntityTypeConfiguration 的 Edm 信息?(五)
直接贴代码了: NewsInfo 实体类: public class NewsInfo { public int NewsInfoId { get; set; } public string News ...
- Python platform 模块
Python platform 模块 platform 模块用于查看当前操作系统的信息,来采集系统版本位数计算机类型名称内核等一系列信息. 使用方法: import platform # 获取操作系统 ...
- 算法初步---基本的数据结构(java为例)
最近搞算法,觉得超级吃力的,一直以为数学好的,数学可以考试满分,算法一定没什么问题,贱贱地,我发现我自己想多了,还是自己的基础薄弱吧,今天我来补补最基础的知识. 算法(Algorithm)是指解题方案 ...
- MySQL出现Waiting for table metadata lock的原因以及解决方法(转)
MySQL在进行alter table等DDL操作时,有时会出现Waiting for table metadata lock的等待场景.而且,一旦alter table TableA的操作停滞在Wa ...
- Linux学习笔记之scp远程拷贝文件
0x00 拷贝本机/home/administrator/test整个目录至远程主机192.168.1.100的/root目录下 代码如下: scp -r /home/administrator/te ...
- 对象数组自定义排序--System.Collections.ArrayList.Sort()
使用System.Collections.ArrayList.Sort()对象数组自定义排序 其核心为比较器的实现,比较器为一个类,继承了IComparer接口并实现int IComparer.Com ...
- C# winform打开新窗体显示一段时间 关闭新窗体
1.form1的button事件下: form2 form = new form2(); form.Show(); Thread.Sleep(10000); //form2窗体显示10秒 form. ...
- netcore sdk版本选择
NetCore sdk并不是每个版本都支持VS2017工具,也不是每个版本的sdk版本号和Runtime版本号都一样,这就需要我们在创建某个版本的net core应用时注意: 使用不同版本的vs时需要 ...