C++常见字符串函数(例题详解版)

目录

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

2、子串 substr()函数

3、替换 replace()函数

(1)例子1

(2)例子2

(3)例子3

4、插入 insert()函数

5、添加字符串 append()函数

6、交换字符串 swap()函数

7、 字符串比较函数 compare()

8、字符串大小 size()函数和length()函数


使用字符串要使用:#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;
}

输入:

sdefdwefdadabbababab

输出:

ab在str中的位置:11

ab在str[2]到str[n-1]中的位置:11

ab在str[0]到str[2]中的位置:18446744073709551615

Program ended with exit code: 0

输入:

asdfghjk

输出:

ab在str中的位置:18446744073709551615

ab在str[2]到str[n-1]中的位置:18446744073709551615

ab在str[0]到str[2]中的位置:18446744073709551615

Program ended with exit code: 0

输入:

abfeofihwabab

输出:

ab在str中的位置:0

ab在str[2]到str[n-1]中的位置:9

ab在str[0]到str[2]中的位置:0

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;
}

输入:

asdfghjkl;'/.,mnbvcxz

输出:

返回str[3]及其以后的子串:fghjkl;'/.,mnbvcxz

从ste[2]开始由四个字符组成的子串:dfgh

Program ended with exit code: 0

3、替换 replace()函数

(1)例子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

1

2

(2)例子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)例子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

1

2

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

1

2

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

1

输出:

在字符串str后面添加字符串ABC:diguwhdcowABC

Program ended with exit code: 0

1

2

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

7、 字符串比较函数 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

8、字符串大小 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值