在这篇文章中,将简述String类的方法。
1
| string str("794TD is a good guy.");
|
查找
str.find()
find()
可以接收两个值。分别是要查找的字符(串)和从哪一个位置开始查找。
1 2 3 4 5 6 7 8
| #include <iostream> using namespace std; int main(){ string str("794TD is a good guy."); int position = str.find("794TD"); cout << position; return 0; }
|
此时,程序输出结果为0,是794TD这个子串在str中首次出现的位置。
1 2 3 4 5 6 7 8 9 10 11 12
| #include <iostream> using namespace std; int main(){ string str("794TD is a good guy."); int position = str.find("794TD",5); if(position != str.nops){ cout << position; } else { cout << "Not Exist"; } return 0; }
|
这里我让程序从str中的第五个元素开始查找, is a good guy.
中无法找到查找的字串。find()
此时返回npos
。
npos是string中的一个特殊标记,代表没有匹配到对应的值。
str.find_first_of()
find_first_of()
可以接收两个值。分别是要查找的字符(串)和从哪一个位置开始查找。
使用方法与find一样。
str.find_last_of()
find_first_of()
可以接收两个值。分别是要查找的字符(串)和从哪一个位置开始查找。
使用方法与find一样。
str.find_first_not_of()
find_first_not_of()
可以接收两个值。分别是要查找的字符(串)和从哪一个位置开始查找。
- 返回除查找字符(串)以外的任何一个字符在查找范围中首次出现的位置
使用方法与find一样。
替换
str.replace()
replace()
可以接收四个参数,前两个指定替换范围,第三个是替换字符串,后两个参数是指定字符串中的那几个用于替换
1 2 3 4 5 6 7 8
| #include <iostream> using namespace std; int main(){ string str("794TD is a good guy."); str = str.replace(0,2,"replaceTest",0,5); cout << str; return 0; }
|
1
| str:repla4TD is a good guy.
|
插入
str.insert()
insert()
可以接受三个参数。分别是在哪里插入、插入字符(串)和将字符串的前几个插入。
1 2 3 4 5 6 7 8
| #include <iostream> using namespace std; int main(){ string str("794TD is a good guy."); str.insert(0,"insertTest ",4); cout << str; return 0; }
|
1
| 输出为:inse794TD is a good guy.
|
删除
str.erase()
erase()
可以接收两个值,分别是开始删除的位置和向后删除几个。
1 2 3 4 5 6 7 8 9 10
| #include <iostream> using namespace std; int main(){ string str("794TD is a good guy."); str.erase(0,5); cout << str<< endl; str.erase(5); cout << str; return 0; }
|
1 2 3
| 输出结果: is a good guy. is a
|
交换
str.swap()
swap()
接收一个与之交换的字符串。
1 2 3 4 5 6 7 8 9
| #include <iostream> using namespace std; int main(){ string str("794TD is a good guy."); string str2("This is str2."); str.swap(str2); cout << str<< endl<< str2; return 0; }
|
1 2
| str: This is str2. str2: 794TD is a good guy.
|
子串
str.substr()
substr()
接收两个参数,分别为起始位置一个是结束位置。
1 2 3 4 5 6 7 8
| #include <iostream> using namespace std; int main(){ string str("794TD is a good guy."); cout << str.substr(0,5)<< endl; cout << str.substr(5); return 0; }
|
删除
str.clear()
清除str中的数据。
str.erase()
erase()
接收两个参数,分别是开始位置和擦除长度。
1 2 3 4 5 6 7 8 9 10 11
| #include <iostream> #include <cstring> using namespace std; int main(){ string str("794TD is a good guy."); str.erase(6,10); cout << str << endl; str.erase(2); cout << str; return 0; }
|
其他
str.size && str.length
返回str的长度。
1 2 3 4 5 6 7
| _NODISCARD size_type length() const noexcept { return _Get_data()._Mysize; }
_NODISCARD size_type size() const noexcept { return _Get_data()._Mysize; }
|
二者的代码一致。size()为C++加入STL后为兼容而引入的一种方法。
str.at()
用途与str[n]
一致,但溢出时会报错。
str.empty()
检查字符串是否为空,为空返回1,不为空返回0。
str.c_str()
将str中的内容转换成一个C字符串。
1 2 3 4 5 6 7 8 9 10 11
| #include <iostream> #include <cstring> using namespace std; int main(){ string str("794TD is a good guy."); char cStr[30]={'\0'}; strcpy(cStr,str.c_str()); cout << cStr; return 0; }
|