Length of the greeting:13Is the greeting empty?NoCharacter at position 7: W
Substringfrom position 7with length 5:WorldPosition of 'World'in the greeting:7Modified greeting:Hello, C++!
std::string 成员函数汇总表
下面是一个常见的 std::string 成员函数的汇总:
函数名
描述
示例代码
size()
返回字符串的长度(字符数)。
std::cout << str.size();
length()
与 size() 相同,返回字符串的长度。
std::cout << str.length();
empty()
判断字符串是否为空。
std::cout << (str.empty()
? "Yes" : "No");
operator[]
访问字符串中指定位置的字符。
std::cout << str[0];
at()
访问字符串中指定位置的字符(带边界检查)。
std::cout << str.at(0);
substr()
返回从指定位置开始的子字符串。
std::string sub = str.substr(0,
5);
find()
查找子字符串在字符串中的位置。
std::cout << str.find("sub")
<< std::endl;
rfind()
从字符串末尾开始查找子字符串的位置。
std::cout << str.rfind("sub")
<< std::endl;
replace()
替换字符串中的部分内容。
str.replace(pos, length, "new_substring");
append()
在字符串末尾添加内容。
str.append(" more");
insert()
在指定位置插入内容。
str.insert(pos, "inserted");
erase()
删除指定位置的字符或子字符串。
str.erase(pos, length);
clear()
清空字符串。
str.clear();
c_str()
返回 C 风格的字符串(以 null 结尾)。
const char* cstr = str.c_str();
data()
返回指向字符数据的指针(C++11 及之后的版本)。
const char* data = str.data();
compare()
比较两个字符串。
int result = str.compare("other");
find_first_of()
查找第一个匹配任意字符的位置。
size_t pos = str.find_first_of("aeiou");
find_last_of()
查找最后一个匹配任意字符的位置。
size_t pos = str.find_last_of("aeiou");
find_first_not_of()
查找第一个不匹配任意字符的位置。
size_t pos = str.find_first_not_of("aeiou");
find_last_not_of()
查找最后一个不匹配任意字符的位置。
size_t pos = str.find_last_not_of("aeiou");
实例
#include <iostream> #include <string> int main(){ std::string str ="Hello, World!";
Length:13Is empty?NoFirst character: H
Character at position 7: W
Substringfrom position 7with length 5:WorldPosition of 'World':7Modifiedstring:Hello, C++!Appendedstring:Hello, C++!How are you?String after insert:Hello,BeautifulC++!How are you?String after erase:Hello, C++!How are you?String after clear:Empty
C-style string:Hello, C++!Comparison result:0First vowel at position:1Last vowel at position:4First non-vowel at position:0Last non-vowel at position:10