string类的常用方法
时间: 2023-06-24 18:09:08 浏览: 51
string类是C++标准库中的一个类,用于处理字符串。下面列出了一些常用的string类方法:
1. length():返回字符串的长度。
2. size():同length(),返回字符串的长度。
3. empty():判断字符串是否为空,返回一个bool值。
4. clear():清空字符串。
5. substr(start, length):返回从start位置开始的长度为length的子串。
6. append(str):在字符串末尾添加str。
7. insert(pos, str):在指定位置pos插入字符串str。
8. erase(start, length):从start位置开始删除长度为length的子串。
9. find(str):查找字符串中第一个出现str的位置,返回该位置的索引值。
10. rfind(str):查找字符串中最后一个出现str的位置,返回该位置的索引值。
11. replace(start, length, str):替换从start位置开始的长度为length的子串为字符串str。
12. c_str():将字符串转换为C风格字符串。
13. operator[]:访问字符串中指定位置的字符。
14. compare(str):比较字符串与str是否相等,返回一个int值,相等返回0,否则返回非0值。
15. getline(istream&, string&):从输入流中读取一行字符串,并存储到string对象中。
以上是一些常用的string类方法,还有其他较少使用的方法可以查看C++官方文档。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)