string类是模板类:
typedef basic_string<char>string
使用string类包含头文件:
#include<string>
string对象的初始化:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1("Hello");
cout << s1 << endl;
string s2(8,'x'); //代表s2里面有8个 x
cout << s2 << endl;
string month="Match";
cout << month << endl;
string s;
s='n';
cout << s << endl;
return 0;
}
运行结果:
string对象的长度用函数length()读取:
string s("Hello");
cout << s.length() << endl;
string 的赋值和连接:
·用 = 赋值
string s1("cat"), s2;
s2 = s1;
·用 assign 成员函数复制
string s1("cat"), s3;
s3.assign(s1);
·用 assign 成员函数部分复制
string s1("catpig"), s4;
s4.assign(s1, 1, 3); //从s1中下标为1的字符开始复制3个字符
·单个字符复制
s2[5] = s1[3] = 'a';
·逐个访问string对象中的字符
string s1("Hello");
for(int i=0;i<s1.length();i++)
cout << s1.at(i) << endl;
//成员函数at会做范围检查,如果超出范围,会抛出 out_of_range异常,而下标运算符[]不做范围检查
·用 + 运算符连接字符串
string s1("good"), s2("moring!");
s1 += s2;
cout << s1;
·用成员函数append连接字符串
string s1("good"), s2("moring!");
s1.append(s2);
cout << s1;
s2.append(s1, 3, s1.size()); //s1.size(), s1字符串
cout << s2;
//下标为3开始,s1.size()个字符,如果字符串内没有足够的字符,则复制到字符串的最后一个字符
比较string:
·用关系运算符比较string的大小
==, >, >=, <, <=, !=
//返回值都是bool类型,成立返回true,否则返回false
string s1("hello"), s2("hello"), s3("hell");
bool b = (s1 == s2);
cout << b << endl; //运行结果为 1
b = (s1 == s3)
cout << b << endl; //运行结果为 0
b = (s1 > s3)
cout << b << endl; //运行结果为 1
·用成员函数compare比较string的大小
string s1("hello"), s2("hello"), s3("hell");
int f1 = s1.compare(s2); //运行结果为 0
int f2 = s1.compare(s3); //运行结果为 1
int f3 = s3.compare(s1); //运行结果为 -1
int f4 = s1.compare(1, 2, s3, 0, 3); //运行结果为 -1
//将s1从下标为1开始的2个字符与s3的从0开始的3个字符比较
int f5 = s1.compare(0, s1.size(), s3); //运行结果为 1
cout << f1 << endl << f2 << endl << f3 << endl;
cout << f4 << endl << f5 << endl;
实现代码为:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1("hello"), s2("hello"), s3("hell");
bool b = (s1 == s2);
cout << b << endl;
b = (s1 == s3);
cout << b << endl;
b = (s1 > s3);
cout << b << endl;
int f1 = s1.compare(s2);
int f2 = s1.compare(s3);
int f3 = s3.compare(s1);
int f4 = s1.compare(1, 2, s3, 0, 3);
//将s1从下标为1开始的2个字符与s3的从0开始的3个字符比较
int f5 = s1.compare(0, s1.size(), s3);
cout << f1 << endl << f2 << endl << f3 << endl;
cout << f4 << endl << f5 << endl;
return 0;
}
运行结果为:
找子串:
·成员函数substr
string s1("hello world"), s2;
s2 = s1.substr(4,5); //从下边4开始的5个字符
cout << s2 << endl; //运行结果为 o wor
交换 string:
·成员函数swap
string s1("hello world"), s2("really");
s1.swap(s2);
cout << s1 << endl;
cout << s2 << endl;
运行结果为:
寻找string中的字符:
·成员函数find()
string s1("hello world");
s1.find("lo");
//在s1中从前向后查找,“lo”第一次出现的地方,如果找到,返回“lo”开始的位置,即l所在位置的下标。如果找不到,返回string::npos(string中定义的静态常量)
·成员函数rfind()
string s1("hello world");
s1.rfind("lo");
//在s1中从后向前查找,“lo”第一次出现的地方,如果找到,返回“lo”开始的位置,即l所在位置的下标。如果找不到,返回string::npos(string中定义的静态常量)
·成员函数find_first_of()
string s1("hello world");
s1.find_first_of("abcd");
//在s1中从前向后查找,“abcd”中任何一个字符第一次出现的地方,如果找到,返回找到字母的位置。如果找不到,返回string::npos(string中定义的静态常量)
·成员函数find_last_of()
string s1("hello world");
s1.find_last_of("abcd");
//在s1中查找,“abcd”中任何一个字符最后一次出现的地方,如果找到,返回找到字母的位置。如果找不到,返回string::npos(string中定义的静态常量)
·成员函数find_first_not_of()
string s1("hello world");
s1.find_first_not_of("abcd");
//在s1中从前向后查找不在“abcd”中的字母第一次出现的地方,如果找到,返回找到字母的位置。如果找不到,返回string::npos(string中定义的静态常量)
代码实现为:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1("hello worlld");
cout << s1.find("ll") << endl;
cout << s1.find("abc") << endl;
cout << s1.rfind("ll") << endl;
cout << s1.rfind("abc") << endl;
cout << s1.find_first_of("abcde") << endl;
cout << s1.find_first_of("abc") << endl;
cout << s1.find_last_of("abcde") << endl;
cout << s1.find_last_of("abc") << endl;
cout << s1.find_first_not_of("abcde") << endl;
cout << s1.find_first_not_of("hello world") << endl;
cout << s1.find_last_not_of("abcde") << endl;
cout << s1.find_last_not_of("hello world") << endl;
return 0;
}
运行结果为:
删除string中的字符:
·成员函数erase()
string s1("hello worlld");
s1.erase(5); //去掉下标5及以后的字符
cout << s1;
cout << s1.length();
cout << s1.size(); //运行结果为 hello55
替换string中的字符:
·成员函数replace()
string s1("hello world");
s1.replace(2,3,"haha"); //将s1中下标2开始的3个字符换成"haha"
cout << s1; //运行结果为 hehaha world
s1.replace(2,3,"haha",1,2); //将s1中下标2开始的3个字符换成"haha" 中下标1开始的2个字符
cout << s1; //运行结果为 heah world
在string中插入字符:
·成员函数insert()
string s1("hello worlld");
string s2("show insert");
s1.insert(5,s2); //将s2插入s1下标5的位置
cout << s1 << endl; //运行结果为 helloshow insert worlld
s1.insert(2,s2,5,3);//将s2中下标5开始的3个字符插入s1下标2的位置
cout << s1 << endl;//运行结果为 heinslloshow insert worlld