String
2025年1月26日大约 2 分钟
String
string的method
(generated by ChatGPT)
Method | Description | Example |
---|---|---|
length() or size() | Returns the number of characters in the string. | s.length(); or s.size(); |
empty() | Checks if the string is empty. | s.empty(); |
clear() | Clears the string, making it empty. | s.clear(); |
append(str) | Appends str to the end of the string. | s.append(" world"); |
operator+= | Concatenates another string or character. | s += " world"; |
insert(pos, str) | Inserts str at position pos . | s.insert(5, " hello"); |
erase(pos, len) | Erases len characters from position pos . | s.erase(5, 3); |
replace(pos, len, str) | Replaces len characters starting at pos with str . | s.replace(5, 3, "world"); |
substr(pos, len) | Returns a substring starting at pos with len characters. | s.substr(0, 5); |
find(str) | Finds the first occurrence of str and returns its position. Returns std::string::npos if not found. | s.find("hello"); |
rfind(str) | Finds the last occurrence of str . | s.rfind("hello"); |
find_first_of(str) | Finds the first occurrence of any character in str . | s.find_first_of("aeiou"); |
find_last_of(str) | Finds the last occurrence of any character in str . | s.find_last_of("aeiou"); |
find_first_not_of(str) | Finds the first character not in str . | s.find_first_not_of("abc"); |
find_last_not_of(str) | Finds the last character not in str . | s.find_last_not_of("abc"); |
compare(str) | Compares the string with str . Returns 0 if equal, <0 if less, >0 if greater. | s.compare("hello"); |
c_str() | Returns a C-style null-terminated character array. | const char* c = s.c_str(); |
data() | Returns a pointer to the underlying character array (similar to c_str() but not null-terminated). | const char* d = s.data(); |
at(pos) | Returns the character at position pos (with bounds checking). | char c = s.at(5); |
operator[] | Returns the character at position pos (no bounds checking). | char c = s[5]; |
push_back(ch) | Appends a character ch to the end of the string. | s.push_back('!'); |
pop_back() | Removes the last character from the string. | s.pop_back(); |
resize(n) | Resizes the string to contain n characters. | s.resize(10); |
swap(str) | Swaps the contents of the string with str . | s.swap(otherString); |
begin() | Returns an iterator to the beginning of the string. | auto it = s.begin(); |
end() | Returns an iterator to the end of the string. | auto it = s.end(); |
rbegin() | Returns a reverse iterator to the beginning of the reversed string. | auto it = s.rbegin(); |
rend() | Returns a reverse iterator to the end of the reversed string. | auto it = s.rend(); |
to_string(val) | Converts a number (int , float , etc.) to a string. | std::to_string(123); |
stoi(str) | Converts a string to an integer. | int num = std::stoi("123"); |
stof(str) | Converts a string to a floating-point number. | float num = std::stof("123.45"); |
getline(cin, str) | Reads an entire line of input into the string. | std::getline(std::cin, s); |
注意rbegin的含义是reversed的string起始字符串的迭代器
Note:
string 风格的字符串用printf("%s")打印会出现乱码,因为这个不适合string风格。想要用printf的话,需要这么写:printf("%d",str.c_str());即调用成员函数c_str();