string
列出一些与vector有差别的成员函数:
substr:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str;
str = "asdfghj";
cout << str.substr(2) << endl;//1.从str[2]起到结束
cout << str.substr(2, 4) << endl;//2.从str[2]起取四个字符
cout << str.substr(2, 100) << endl;//3.若后一个参数超过了字符串总长,则同1
return 0;
}
compare:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str;
str = "asdfghj";
cout << str.compare("bc") << endl;//若str小于compare的参数,返回-1
cout << str.compare("asdfghj") << endl;//若str和compare的参数相等,返回0
cout << str.compare("ab") << endl;//若str大于compare的参数,返回1
return 0;
}
find:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str;
str = "asdfghjasdfghj";
cout << str.find("asd") << endl;//若查找成功,返回第一个字符的索引
cout << str.find("asdef", str.find("asd") + 1, 3) << endl;
//参数二为查找的起始位置,参数三表示截取待查串的长度
cout << str.find("asdef", str.find("asd") + 1) << endl;//若未找到,返回string::npos
return 0;
}
//rfind类似,只是从反向开始查找
copy:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str;
char s[20];
str = "asdfghjasdfghj";
cout << str << endl;
str.copy(s, 2, 5);//将str[5]起的2个字符拷入s
s[2] = '\0';
cout << s << endl;
return 0;
}
replace:
用法太多ORZ,以后再填坑吧。