1、string的用法
一、初始化
初始化有两种方式,其中使用等号的是拷贝初始化,不使用等号的是直接初始化。
string str1 = "hello world"; // str1 = "hello world" string str2("hello world"); // str2 = "hello world" string str3 = str1; // str3 = "hello world" string str4(str2); // str4 = "hello world" string str5(10,'h'); // str5 = "hhhhhhhhhh" string str6 = string(10,'h'); // str6 = "hhhhhhhhhh" string str7(str1,6); // str7 = "world" 从字符串str1第6个字符开始到结束,拷贝到str7中 string str8 = string(str1,6); // str8 = "world" string str9(str1,0,5); // str9 = "hello" 从字符串str1第0个字符开始,拷贝5个字符到str9中 string str10 = string(str1,0,5); // str10 = "hello" char c[] = "hello world"; string str11(c,5); // str11 = "hello" 将字符数组c的前5个字符拷贝到str11中 string str12 = string(c,5); // str12 = "hello"其中:
string str13 = string("hello world",5) // str13 = "hello" 而非 " world"
length和size都可以用来获取
string str = "hello world"; cout << str.length() << str.size(); // 11 11当str.length()与其他类型比较时,建议先强制转换为该类型,否则会意想之外的错误。
比如:-1 > str.length() 返回 true。
三、插入
string str = "hello world"; string str2 = "hard "; string str3 = "it is so happy wow"; //s.insert(pos,n,ch) 在字符串s的pos位置上面插入n个字符ch str.insert(6,4,'z'); // str = "hello zzzzworld" //s.insert(pos,str) 在字符串s的pos位置插入字符串str str.insert(6,str2); // str = "hello hard world" //s.insert(pos,str,a,n) 在字符串s的pos位置插入字符串str中位置a到后面的n个字符 str.insert(6,str3,6,9); // str = "hello so happy world" //s.insert(pos,cstr,n) 在字符串s的pos位置插入字符数组cstr从开始到后面的n个字符 //此处不可将"it is so happy wow"替换为str3 str.insert(6,"it is so happy wow",6); // str = "hello it is world"
string str = "hello world"; string str2 = "hard "; string str3 = "it is so happy wow"; //s.replace(p0,n0,n,ch) 删除p0开始的n0个字符,然后在p0处插入n个字符ch str.replace(0,6,4,'z'); // str = "zzzzworld" //s.replace(p0,n0,str) 删除从p0开始的n0个字符,然后在p0处插入字符串str str.replace(0,6,str2); // str = "hard world" //s.replace(p0,n0,str,pos,n) 删除p0开始的n0个字符,然后在p0处插入字符串str中从pos开始的n个字符 str.replace(0,6,str3,6,9); // str = "so happy world" //s.replace(p0,n0,cstr,n) 删除p0开始的n0个字符,然后在p0处插入字符数组cstr的前n个字符 //此处不可将"it is so happy wow"替换为str3 str.replace(0,6,"it is so happy wow",6); // str = "it is world"
string str = "hello world"; string str2 = "hard "; string str3 = "it is so happy wow"; //s.append(n,ch) 在当前字符串结尾添加n个字符c str.append(4,'z'); // str = "hello worldzzzz" //s.append(str) 把字符串str连接到当前字符串的结尾 str.append(str2); // str = "hello worldhard " //s.append(str,pos,n) 把字符串str中从pos开始的n个字符连接到当前字符串的结尾 str.append(str3,6,9); // str = "hello worldso happy " //append(cstr,int n) 把字符数组cstr的前n个字符连接到当前字符串结尾 //此处不可将"it is so happy wow"替换为str3 str.append("it is so happy wow",6); // str = "hello worldit is "
六、赋值
string str; string temp = "welcome to my blog"; //s.assign(n,ch) 将n个ch字符赋值给字符串s str.assign(10,'h'); // str = "hhhhhhhhhh" //s.assign(str) 将字符串str赋值给字符串s str.assign(temp); // str = "welcome to my blog" //s.assign(str,pos,n) 将字符串str从pos开始的n个字符赋值给字符串s str.assign(temp,3,7); // str = "come to" //s.assaign(cstr,n) 将字符数组cstr的前n个字符赋值给字符串s //此处不可将"it is so happy wow"替换为temp str.assign("welcome to my blog",7); //welcome
七、删除(erase)
string str = "welcome to my blog"; //s.erase(pos,n) 把字符串s从pos开始的n个字符删除 str.erase(11,3); // str = "welcome to blog"
八、剪切(substr)
string str = "The apple thinks apple is delicious"; //s.substr(pos,n) 得到字符串s位置为pos后面的n个字符组成的串 string s1 = str.substr(4,5); // s1 = "apple" //s.substr(pos) 得到字符串s从pos到结尾的串 string s2 = str.substr(17); // s2 = "apple is delicious"
九、比较(compare)
string str1 = "small leaf"; string str2 = "big leaf"; //s.compare(str) 比较当前字符串s和str的大小 cout << str1.compare(str2); // 1 //s.compare(pos,n,str) 比较当前字符串s从pos开始的n个字符与str的大小 cout << str1.compare(2,7,str2); // -1 //s.compare(pos,n0,str,pos2,n) 比较当前字符串s从pos开始的n0个字符与str中pos2开始的n个字符组成的字符串的大小 cout << str1.compare(6,4,str2,4,4); // 0 //s.compare(pos,n0,cstr,n) 比较当前字符串s从pos开始的n0个字符与字符数组cstr中前n个字符的大小 //此处不可将"big leaf"替换为str2 cout << str1.compare(6,4,"big leaf",4); // 1
交换两个字符串的值
string str1 = "small leaf"; string str2 = "big leaf"; //或者str1.swap(str2) ,输出结果相同 swap(str1,str2); // str1 = "big leaf" str2 = "small leaf" swap(str1[0],str1[1]); // str1 = "ibg leaf"
十一、反转
反转字符串
string str = "abcdefghijklmn"; reverse(str.begin(),str.end()); // str = "nmlkjihgfedcba"
to_string(val) //把val转换成string stoi(s,p,b) //把字符串s从p开始转换成b进制的int stol(s,p,b) //把字符串s从p开始转换成b进制的long stoul(s,p,b) //把字符串s从p开始转换成b进制的unsigned long stoll(s,p,b) //把字符串s从p开始转换成b进制的long long stoull(s,p,b) //把字符串s从p开始转换成b进制的unsigned long long stof(s,p) //把字符串s从p开始转换成float stod(s,p) //把字符串s从p开始转换成double stold(s,p) //把字符串s从p开始转换成long double
十三、迭代器(iterator)
string str = "abcdefghijklmn"; //s.begin() 返回字符串s第一个字符的位置 char a = *(str.begin()); // a //s.end() 返回字符串s最后一个字符串的后一个位置 char b = *(str.end()-1); // n //s.rbegin() 返回字符串s最后一个字符的位置 char c = *(str.rbegin()); // n //s.rend() 返回字符串s第一个字符的前一个位置 char d = *(str.rend()-1); // a
十四、查找(find)
string str = "The apple thinks apple is delicious"; //长度34 string key = "apple"; //s.find(str) 查找字符串str在当前字符串s中第一次出现的位置 int pos1 = str.find(key); // 4 //s.find(str,pos) 查找字符串str在当前字符串s的[pos,end]中第一次出现的位置 int pos2 = str.find(key, 10); // 17 //s.find(cstr,pos,n) 查找字符数组cstr前n的字符在当前字符串s的[pos,end]中第一次出现的位置 //此处不可将"delete"替换为str2(如果定义str2 = "delete") int pos3 = str.find("delete", 0, 2); // 26 //s.find(ch,pos) 查找字符ch在当前字符串s的[pos,end]中第一次出现的位置 int pos4 = str.find('s', 0); // 15
//s.rfind(str) 查找字符串str在当前字符串s中最后一次出现的位置 int pos5 = str.rfind(key); // 17 //s.rfind(str,pos) 查找字符串str在当前字符串s的[0,pos+str.length()-1]中最后一次出现的位置 int pos6 = str.rfind(key, 16); // 4 //s.rfind(cstr,pos,n) 查找字符数组cstr前n的字符在当前字符串s的[0,pos+n-1]中最后一次出现的位置 //此处不可将"apple"替换为key int pos7 = str.rfind("apple", 40, 2); // 17 //s.rfind(ch.pos) 查找字符ch在当前字符串s的[0,pos]中最后一次出现的位置 int pos8 = str.rfind('s', 30); // 24
find_xxx_of
string str = "The early birds catch the warm"; //长度30 string key = "aeiou";
// s.find_first_of(str) 查找字符串str中的任意字符在当前字符串s中第一次出现的位置 int pos1 = str.find_first_of(key); // 2 //s.find_first_of(str,pos) 查找字符串str中的任意字符在当前字符串s的[pos,end]中第一次出现的位置 int pos2 = str.find_first_of(key, 10); // 11 //s.find_first_of(cstr,pos,n) 查找字符串str前n个任意字符在当前字符串s的[pos,end]中第一次出现的位置 //此处不可将"aeiou"替换为key int pos3 = str.find_first_of("aeiou", 7, 2); // 17 //s.find_first_of(ch,pos) 查找字符ch在当前字符串s的[pos,end]中第一次出现的位置 int pos4 = str.find_first_of('r', 0); // 6
//s.find_first_not_of(str) 查找字符串str之外的任意字符在当前字符串s中第一次出现的位置 int pos1 = str.find_first_not_of(key); // 0 //s.find_first_not_of(str,pos) 查找字符串str之外的任意字符在当前字符串s的[pos,end]中第一次出现的位置 int pos2 = str.find_first_not_of(key, 10); // 10 //s.find_first_not_of(cstr,pos,n) 查找字符串str前n个之外任意字符在当前字符串s的[pos,end]中第一次出现的位置 //此处不可将"aeiou"替换为key int pos3 = str.find_first_not_of("aeiou", 7, 2); // 7 //s.find_first_not_of(str) 查找字符ch之外任意字符在当前字符串s的[pos,end]中第一次出现的位置 int pos4 = str.find_first_not_of('r', 0); // 0
//s.find_last_of(str) 查找字符串str中的任意字符在当前字符串s中最后一次出现的位置 int pos1 = str.find_last_of(key); // 27 //s.find_last_of(str,pos) 查找字符串str中的任意字符在当前字符串s的[0,pos]中最后一次出现的位置 int pos2 = str.find_last_of(key, 15); // 11 //s.find_last_of(cstr,pos,n) 查找字符串str前n个任意字符在当前字符串s的[0,pos]中最后一次出现的位置 //此处不可将"aeiou"替换为key int pos3 = str.find_last_of("aeiou", 20, 2); // 17 //s.find_last_of(str) 查找字符ch在当前字符串s的[0,pos]中最后一次出现的位置 int pos4 = str.find_last_of('r', 30); // 28
//s.find_last_not_of(str) 查找字符串str之外的任意字符在当前字符串s中最后一次出现的位置 int pos1 = str.find_last_not_of(key); // 29 //s.find_last_not_of(str,pos) 查找字符串str之外的任意字符在当前字符串s的[0,pos]中最后一次出现的位置 int pos2 = str.find_last_not_of(key, 15); // 15 //s.find_last_not_of(cstr,pos,n) 查找字符串str前n个之外任意字符在当前字符串s的[0,pos]中最后一次出现的位置 //此处不可将"aeiou"替换为key int pos3 = str.find_last_not_of("aeiou", 20, 2); // 20 //s.find_last_not_of(str) 查找字符ch之外任意字符在当前字符串s的[0,pos]中最后一次出现的位置 int pos4 = str.find_last_not_of('r', 30); // 29