Modifiers :
-
operator+=
- Append to string (public member function )
-
append
- Append to string (public member function )
-
push_back
- Append character to string (public member function )
-
assign
- Assign content to string (public member function )
-
insert
- Insert into string (public member function )
-
erase
- Erase characters from string (public member function )
-
replace
- Replace portion of string (public member function )
-
swap
- Swap string values (public member function )
-
pop_back
- Delete last character (public member function )
Append to string
字符串的添加 str.append(); //
Extends the
string by appending additional characters at the end of its current value:
// appending to string
#include <iostream>
#include <string>
int main ()
{
std::string str;
std::string str2="Writing ";
std::string str3="print 10 and then 5 more";
// used in the same order as described above:
str.append(str2); // "Writing "
str.append(str3,6,3); // "10 "
str.append("dots are cool",5); // "dots "
str.append("here: "); // "here: "
str.append(10u,'.'); // ".........."
str.append(str3.begin()+8,str3.end()); // " and then 5 more"
str.append<int>(5,0x2E); // "....."
std::cout << str << '\n';
return 0;
}
Output:
Writing 10 dots here: .......... and then 5 more..... |
Insert into string 插入字符。 str.insert();
string (1) | string& insert (size_t pos, const string& str); |
---|---|
substring (2) | string& insert (size_t pos, const string& str, size_t subpos, size_t sublen); |
c-string (3) | string& insert (size_t pos, const char* s); |
buffer (4) | string& insert (size_t pos, const char* s, size_t n); |
fill (5) | string& insert (size_t pos, size_t n, char c); |
std::string ::substr() 提取子字符串
Returns a newly constructed
string
object with its value initialized to a copy of a substring of this object.
|
Output:
generalities live in details. |
字符串的搜索。std::string::find
Find content in string
Searches the
string for the first occurrence of the sequence specified by its arguments.
找到字符串在string中第一次出现的位置
找到字符串在string中第一次出现的位置
When pos is specified, the search only includes characters at or after position pos, ignoring any possible occurrences that include characters before pos.
Notice that unlike member find_first_of, whenever more than one character is being searched for, it is not enough that just one of these characters match, but the entire sequence must match.
返回值return
The position of the first character of the first match.
If no matches were found, the function returns string::npos. 没有找到 则返回 sting ::npos
size_t is an unsigned integral type. 无符号型
If no matches were found, the function returns string::npos. 没有找到 则返回 sting ::npos
size_t is an unsigned integral type. 无符号型