string类的常用构造函数
#include<iostream>#include<string>using namespace std;int main(){ string s1; //空字符串
string s2(10, '@'); //10个@
string s3("Hello World"); //hello world //等价于string s3 = "Hello World"
string s4(s3); //s4和s3一样
string s5("hello world", 20); //hello world //将hello world存入s5,最多存储20个字节
string s6("Hello world", 2, 20); //llo world //将Hello world的第2位作为字符串的开头,存到s6,最多存储20个字节}
使用string类时,必须包含头文件以及using namespace std
string类常用函数
#include<iostream>#include<string>using namespace std;int main(){ string s("Hello World"); string c("GoodBye World");
cout << s.length() << endl; //11 返回字符串长度
cout << s.size() << endl; //11 和length()一样
cout << s.capacity() << endl; //15 返回s3的容量
s.resize(10); //Hello Worl 设置s的大小为10 //若大小大于当前字符串长度,\0来填充
s.resize(20, '!'); //Hello Worl!!!!!!!!!! //若大小大于当前字符串长度,!来填充
s.reserve(10); //Hello Worl //设置s的容量为10,不会填充数据
s.swap(c); //GoodBye World 交换s和c
s.push_back('!'); //GoodBye World! //在s末尾添加 ! ,参数必须是字符形式
s.append("HHH"); //GoodBye World!HHH //在s末尾添加HHH,参数必须是字符串形式
s.clear(); //清空s}
assign函数
#include<iostream>#include<string>using namespace std;int main(){ string s("Hello World");
s.assign("GoodBye"); //GoodBye //清空s,并设置为GoodBye
s.assign("GoodBye", 4); //Good //清空s,并设置为Good,保留两个字符
s.assign("GoodBye", 1, 2); //oo //清空s,并设置为GoodBye的第1个字符开始,保留2个字符
s.assign(5, '!'); //!!!!! //清空s,并设置为!!!!!}
插入
#include<iostream>#include<string>using namespace std;int main(){ string s("Hello World");
s.insert(2, 2, '!'); //He!!llo World //在s下标为2的位置插入2个!
s.insert(2, "AAA"); //HeAAA!!llo World //在s下标为2的位置插入AAA
s.insert(2, "BCD", 1); //HeBAAA!!llo World //在s下标为2的位置插入 BCD 中的1个字符
s.insert(2, "EFGH", 1, 2); //HeFGBAAA!!llo World //在s下标为2的位置插入 BCD 中从位置1开始的2个字符}
删除
#include<iostream>#include<string>using namespace std;int main(){ string s("Hello World");
s.erase(8); //Hello Wo //从下标为8的位置开始,之后全部删除
s.erase(2, 1); //Helo Wo //从下标为2的位置开始,删除1个字符
s.replace(2, 4, "ABCD"); //HeABCDo //从下标为2的位置开始,替换4个字节为 ABCD
s.clear(); //清空s
cout<<s.empty()<<endl; //1 判空,返回0/1}
反转
#include<iostream>#include<string>using namespace std;int main(){ string s("Hello World"); reverse(s.begin(), s.end()); //dlroW olleH //从s的开始到结束字符反转}
查找
#include<iostream>#include<string>using namespace std;int main(){ string s("Hello World");
cout << s.find("l") << endl; //2 //查找l,返回第一次出现位置
cout << s.find("World") << endl; //6 //查找World,返回第一次出现位置
cout << s.find('l', 3) << endl; //3 //从下标为3的位置查找l,返回第一次出现位置
cout << s.find("Worll", 2, 4) << endl; //6 //从下标为2的位置查找 Worll 的前四个字符,返回第一次出现位置}
s.rfind():从尾部查找
s.find_first_of():查找是否含有字串中任何一个字符,并返回第一次出现位置
s.find_last_of():从尾部开始查找是否含有字串中任何一个字符,并返回第一次出现位置
拷贝
#include<iostream>#include<string>using namespace std;int main(){ string s1("Hello World"); string s2;
s2 = s1.substr(2); //llo World //提取从s1下标为2开始到末尾,并赋值给s2
s2 = s1.substr(2, 3);//llo //提取从s1下标为2开始,长度为3字节,并赋值给s2}
比较
#include<iostream>#include<string>using namespace std;int main(){ string s("Hello World");
cout << s.compare("Hello World") << endl; //0 //完全一样返回0,字符串大于s返回<0的值,字符串小于s返回>0的值
cout << s.compare(0, 2, "HHH", 0, 3) << endl; //1 //s的下标为0开始的两个字符 和 HHH的下标为0开始的3个字符比较}