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