string头文件的详细使用

一、初始化
初始化有两种方式,其中使用等号的是拷贝初始化,使用括号的是直接初始化。

1.字符串直接初始化

string str1="hello world";

string str2("hello world")

2.复制初始化

string str3=str1;//str3="hello world"

string str4(str1);//str4="hello world"

3.重复字符初始化

string str5=string(5,'a')//str5="aaaaa"

string str6(5,'a');//str6="aaaaa"

4.片段初始化

string str7(str1,6);//意思是从str1的第6个字符开始到最后,str7="world"

string str8=string(str1,6);//同上

string str9(str1,0,5);//意思是从str1的第0个字符开始,数5个字符初始化str9,str9="hello"

string str10=string(str1,0,5);//同上

char c[]="hello world";

string str11(c,5);//意思是用c的前五个字符初始化str11;

string str12=string(c,5);//同上

注意,

string str13=string("hello world",5);//此时应该把hello world作为字符数组,str13="hello"而非" world"

二、获取长度(length、size)
length()函数与size()函数均可获取字符串长度。

string str = "hello world";

cout << str.length() << str.size(); // 11 11

当str.length()与其他类型比较时,建议先强制转换为该类型,否则会意想之外的错误。

比如:-1 > str.length() 返回 true。

三.插入(insert)

string str1("hello world");

string str2("hard");

string str3("i am so happy now");

//s.insert(m,n,'c'),在s字符串的第m个字符上添加n个'c';

str1.insert(6,4,'z')//str1 = "hello zzzzworld";

//s.insert(m,str2),在s字符串上的第m位置上添加字符串str2;

str1.insert(6,str2)//str1="hello hardworld";

//s.insert(m,str2,q,n),在s字符串上的第m位置上添加字符串str2的从第q位开始往后数n个字符

str1.insert(6,str2,5,9)//str1="hello so happy world";

//s.insert(m,cstr,n),在s字符串上第m位置上添加字符数组cstr从开始往后的n个字符

str1.insert(6,"i am so happy wow",5);/str1="hello i am world";//注意这里是数组而不是string类型,"i am so happy wow"不能由str3替换

四、替换(replace)
替换与插入对应,对比理解更为简单。

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"

五、添加(append)
append函数用在字符串的末尾添加字符和字符串。(同样与插入、替换对应理解)

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 "

六、赋值(assign)
赋值也是一种初始化方法,与插入、替换、添加对应理解较为简单。

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)
两个字符串自左向右逐个字符相比(按ASCII值大小相比较),直到出现不同的字符或遇’\0’为止。

若是遇到‘\0’结束比较,则长的子串大于短的子串,如:“9856” > “985”。

如果两个字符串相等,那么返回0,调用对象大于参数返回1,小于返回-1。

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

十、交换(swap)
交换两个字符串的值。

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"

十一、反转(reverse)
反转字符串。

string str = "abcdefghijklmn";

reverse(str.begin(),str.end()); // str = "nmlkjihgfedcba"

十二、数值转化(sto*)
详情参见前篇文章《int、string 类型相互转换》。

(本小白调试时,只有将p设置为0或nullptr才运行成功)

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类型中迭代器的使用》。

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)
14.1 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

14.2 rfind函数
//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

14.3 find_xxx_of函数
string str = "The early birds catch the warm"; //长度30

string key = "aeiou";

find_first_of

// 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

find_first_not_of

//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

find_last_of

//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

find_last_not_of

//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

————————————————

 

String的遍历

public static void main(String[] args)
{
String[] str=new String[3];
for (int i = 0; i < str.length; i++) {
str[i]="i=;"+i;
}
// 方法一:for循环
for (int i = 0; i < str.length; i++) {
System.out.println(str[i]);
}
// 方法二:foreach
for (String string : str) {
System.out.println(string);
}
// 方法三:迭代器遍历
List list=Arrays.asList(str);
System.out.println(str);

}
————————————————
版权声明:本文为CSDN博主「aFakeProgramer」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/usstmiracle/article/details/107688331

转自csdn aFakeProgramer的文章,对string头文件常用函数讲的很透彻,有助于记忆,为了方便之后复习,加入博客

 

posted @ 2023-04-21 08:41  _夸夸  阅读(913)  评论(0编辑  收藏  举报