C++常见的字符串处理函数

C++常见的字符串处理函数

#include< string >

1. 应用于查找的find()函数

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string str;
    cin>>str;
    //主要字符串是从0开始计数的
    cout<<"ab在str中的位置:"<<str.find("ab")<<endl;
    //返回第一次出现ab的位置,没有则返回一串乱码
    cout<<"ab在str[2]到str[n-1]中的位置:"<<str.find("ab",2)<<endl;
    //返回第一次从str[2]开始出现ab的位置,没有则返回一串乱码
    cout<<"ab在str[0]到str[2]中的位置:"<<str.rfind("ab",2)<<endl;
    //返回ab在str[0]到str[2]中的位置,没有则返回一串乱码
    return 0;
}

输入1

sdefdwefdadabbababab

输出1

ab在str中的位置:11
ab在str[2]到str[n-1]中的位置:11
ab在str[0]到str[2]中的位置:18446744073709551615
Program ended with exit code: 0

输入2

abfeofihwabab

输出2

ab在str中的位置:0
ab在str[2]到str[n-1]中的位置:9
ab在str[0]到str[2]中的位置:0
Program ended with exit code: 0

输入3

asdfghjk

输出3

ab在str中的位置:18446744073709551615
ab在str[2]到str[n-1]中的位置:18446744073709551615
ab在str[0]到str[2]中的位置:18446744073709551615
Program ended with exit code: 0

2. 子串substr()函数

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string str;
    cin>>str;
    cout<<"返回str[3]及其以后的子串:"<<str.substr(3)<<endl;
    //若小于限制长度则报错
    cout<<"从ste[2]开始由四个字符组成的子串:"<<str.substr(2,4)<<endl;
    //若小于限制长度则报错
    return 0;
}

输入1

asdfghjkl;'/.,mnbvcxz

输出1

返回str[3]及其以后的子串:fghjkl;'/.,mnbvcxz
从ste[2]开始由四个字符组成的子串:dfgh
Program ended with exit code: 0

3. 替换replace()函数

示列1

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string line = "this@ is@ a test string!";
    line = line.replace(line.find("@"), 1, ""); //从第一个@位置替换第一个@为空
    cout<<line<<endl;
    return 0;
}

输出

his is@ a test string!
Program ended with exit code: 0

示列2

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string line = "this@ is@ a test string!";
    line = line.replace(line.begin(), line.begin()+6, "");  //用str替换从begin位置开始的6个字符
    cout << line << endl;
    return 0;
}

示列3

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string line = "this@ is@ a test string!";
    char* str = "12345";
    line = line.replace(0, 5, str); //用str替换从指定位置0开始长度为5的字符串    
    cout << line << endl;
    return 0;
}

输出

12345 is@ a test string!
Program ended with exit code: 0

4. 插入:insert()函数

#include <string>
#include <iostream>
using namespace std;
int main()
{
    string str;
    cin>>str;
    cout<<"从2号位置插入字符串jkl并形成新的字符串返回:"<<str.insert(2, "jkl")<<endl;
    return 0;
}

输入

sdfgh

输出

从2号位置插入字符串jkl并形成新的字符串返回:sdjklfgh
Program ended with exit code: 0

5. 添加字符串:append()函数

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string str;
    cin>>str;
    cout<<"在字符串str后面添加字符串ABC:"<<str.append("ABC")<<endl;
    return 0;
}

输入

diguwhdcow

输出

在字符串str后面添加字符串ABC:diguwhdcowABC
Program ended with exit code: 0

6. 交换字符串:swap()函数

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string str1,str2;
    cin>>str1>>str2;
    cout<<"str1:"<<str1<<endl;
    cout<<"str2:"<<str2<<endl;
    swap(str1, str2);
    cout<<"str1:"<<str1<<endl;
    cout<<"str2:"<<str2<<endl;
}

输入

qwertyui
asdfghjk

输出

str1:qwertyui
str2:asdfghjk
str1:asdfghjk
str2:qwertyui
Program ended with exit code: 0

8. 字符串比较函数:compare()

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string str1,str2;
    cin>>str1>>str2;
    cout<<str1.compare(str2)<<endl;
    return 0;
}

输入

diwguc
aschsdnv

输出

3
Program ended with exit code: 0

7. 字符串大小

size()函数和length()函数

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string str1;
    cin>>str1;
    cout<<str1.size()<<endl;
    cout<<str1.length()<<endl;
    return 0;
}

输入

dchiascnsc

输出

10
10
Program ended with exit code: 0

#include< string.h >

strcpy(s1,s2)

复制字符串s2到s1

strcat(s1,s2)

连接s2到s1的末尾

strlen(s1)

返回字符串s1的长度

strcmp(s1,s2)

若s1和s2是相同的,则返回0,s1< s2,返回值小于0,若s1>s2,返回值大于0

strchr(s1,ch)

返回一个指针,指向字符串s1中字符ch第一次出现的位置

strstr(s1,s2)

返回一个指针,指向字符串s1中字符串s2的第一次出现位置

memcpy (void *dest, const void *src, int size)

从源src所指的内存地址的起始位置开始拷贝n个字节到目标dest所指的内存地址的起始位置中
strcpy与memcpy的区别:
1、复制的内容不同。strcpy只能复制字符串,而memcpy可以复制任意内容,例如字符数组、整型、结构体、类等。
2、复制的方法不同。strcpy不需要指定长度,它遇到被复制字符的串结束符"\0"才结束,所以容易溢出。memcpy则是根据其第3个参数决定复制的长度。
3、用途不同。通常在复制字符串时用strcpy,而需要复制其他类型数据时则一般用memcpy

  • 56
    点赞
  • 253
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Gowi_fly

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值