C++基础-string截取、替换、查找子串函数

  1. 截取子串

    s.substr(pos, n)    截取s中从pos开始(包括0)的n个字符的子串,并返回
    
    s.substr(pos)        截取s中从从pos开始(包括0)到末尾的所有字符的子串,并返回
    
  2. 替换子串

    s.replace(pos, n, s1)    用s1替换s中从pos开始(包括0)的n个字符的子串
    
  3. 查找子串

    s.find(s1)         查找s中第一次出现s1的位置,并返回(包括0)
    
    s.rfind(s1)        查找s中最后次出现s1的位置,并返回(包括0)
    
    s.find_first_of(s1)       查找在s1中任意一个字符在s中第一次出现的位置,并返回(包括0)
    
    s.find_last_of(s1)       查找在s1中任意一个字符在s中最后一次出现的位置,并返回(包括0)
    
    s.fin_first_not_of(s1)         查找s中第一个不属于s1中的字符的位置,并返回(包括0)
    
    s.fin_last_not_of(s1)         查找s中最后一个不属于s1中的字符的位置,并返回(包括0)
    

https://blog.csdn.net/u011974987/article/details/52505004
c++ 提供的string类包含了若干实用的成员函数,大大方便了字符串的增加、删除、更改、查询等操作。

插入字符串
insert()函数可以在string字符串中置顶的位置插入另一个字符串,它的原型为:

string& insert (size_t pos, const string& str);
1
看这个插入的格式我们就能猜想到,pos表示要插入的下标;str表示要插入的字符串,它可以是string变量,也可以是C风格的字符串。

看下面的代码:

#include
#include
using namespace std;
void main(){
string s1, s2, s3;
s1 = s2 = “1234567890”;
s3 = “aaa”;
s1.insert(5, s3);
cout << s1 << endl;
s2.insert(5, “bbb”);
cout << s2 << endl;

system("pause");

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
运行结果:

12345aaa67890
12345bbb67890
请按任意键继续. . .
1
2
3
4
insert()函数的第一个参数有越界的可能,如果越界,则会产生运行时异常。我恶魔你要捕获这个异常。

删除字符串
erase()函数可以删除string变量中的一个字符串,原型为:

string& erase (size_t pos = 0, size_t len = npos);
1
pos 表示要删除的子字符串的起始下标,len表示要删除子字符串的长度。如果不指明len的话,那么直接删除pos到字符串结束处的所有字符(此时len =str.length-pos)。

示例代码如下:

#include
#include
using namespace std;
void main(){
string s1, s2, s3;
s1 = s2 = s3 = “1234567890”;
s2.erase(5);
s3.erase(5, 3);
cout<< s1 <<endl;
cout<< s2 <<endl;
cout<< s3 <<endl;
system(“pause”);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
运行结果:

1234567890
12345
1234590
请按任意键继续. . .
1
2
3
4
在 pos 参数没有越界的情况下, len 参数也可能会导致要删除的子字符串越界。但实际上这种情况不会发生,erase() 函数会从以下两个值中取出最小的一个作为待删除子字符串的长度:

len的值
字符串长度减去 pos 的值。
简单的说,就是待删除字符串最多只能删除到字符串结尾。

提取字符串
substr()函数原型为:

string substr (size_t pos = 0, size_t len = npos) const;
1
pos为要提取的子字符串的起始下标,len为要提取的子字符串的长度。

#include
#include
using namespace std;
void main(){
string s1 = “first second third”;
string s2;
s2 = s1.substr(6, 6);
cout << s1 << endl;
cout << s2 << endl;
system(“pause”);
}
1
2
3
4
5
6
7
8
9
10
11
输出结果为:

first second third
second
请按任意键继续. . .
1
2
3
4
系统对 substr() 参数的处理和 erase() 类似:

如果 pos 越界,会抛出异常;
如果 len 越界,会提取从 pos 到字符串结尾处的所有字符。
字符串的查找
find()函数
find()函数用于string字符串中查找子字符串出现的位置,它的原型为:

size_t find (const string& str, size_t pos = 0) const;
size_t find (const char* s, size_t pos = 0) const;
1
2
第一个参数的表示为待查找的子字符串,它可以是string变量,也可以是C风格的字符串,第二个参数表示开始查找的位置(下标);

/字符串查找替换
void main()
{
string s1 = “apple google apple iphone”;
//从0开始查找"google"的位置
int idx = s1.find(“google”, 0);
cout << idx << endl;

//统计apple出现的次数
int idx_app = s1.find("apple",0);
//npos大于任何有效下标的值
int num = 0;
while (idx_app != string::npos)
{
    num++;
    cout << "找到的索引:" << idx_app << endl;
    idx_app+=5;
    idx_app = s1.find("apple", idx_app);
}

cout << num << endl;
system("pause");

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
输出结果为:

6
找到的索引:0
找到的索引:13
2
请按任意键继续. . .
1
2
3
4
5
find函数最终返回的是子字符串 第一次出现在字符串的其实下标,如果没有查找到子字符串,那么会返回一个无穷大的值 4294967295。统计apple出现的次数。先查找第一次出现的位置,接着
和npos大于任何有效下标的值,来判断,while循环,每次加上自身的长度,最后统计出现的次数。。。

rfind()函数
rfind() 和 find() 很类似,同样是在字符串中查找子字符串,不同的是 find() 函数从第二个参数开始往后查找,而 rfind() 函数则最多查找到第二个参数处,如果到了第二个参数所指定的下标还没有找到子字符串,则返回一个无穷大值4294967295。

#include
#include
using namespace std;
void main(){
string s1 = “first second third”;
string s2 = “second”;
int index = s1.rfind(s2, 6);
if (index < s1.length())
cout << "Found at index : " << index << endl;
else
cout << “Not found” << endl;

system("pause");

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
运行的结果为:

Found at index : 6
请按任意键继续. . .
1
2
find_first_of() 函数
find_first_of() 函数用于查找子字符串和字符串共同具有的字符在再辅传中首先出现的位置。

#include
#include
using namespace std;
int main(){
string s1 = “first second second third”;
string s2 = “asecond”;
int index = s1.find_first_of(s2);
if(index < s1.length())
cout<<"Found at index : "<< index <<endl;
else
cout<<“Not found”<<endl;
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
运行结果为:

Found at index : 3
1
s1 和 s2 共同具有的字符是 ’s’,该字符在 s1 中首次出现的下标是3,故查找结果返回3。
————————————————
版权声明:本文为CSDN博主「徐昊Xiho」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
原文链接:

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值