字符串处理
博客源地址:修能的博客
字符串拼接(Concatenation):
- 使用
"+"
运算符可以将两个字符串进行拼接。 - 使用
append()
函数可以将一个字符串附加到另一个字符串的末尾。
string str1 = "Hello,";
string str2 = "World!";
string str_plus = str1 + str2;
string str_append = str1.append(str2);
cout << str_plus << endl;
cout << str_append << endl;
Hello,World!
Hello,World!
字符串长度(Length):
- 使用
length()
函数可以获取字符串的长度。 - 使用
size()
函数也可以获取字符串的长度。
string str = "Hello,World!";
cout << str.length();
cout << str.size();
12
12
字符串比较(Comparison):
-
使用
==
运算符可以比较两个字符串是否相等。string str1 = "Hello"; string str2 = "hello"; string str3 = "Hello"; printf("%s", str1 == str2 ? "Same\n" : "Different\n"); printf("%s", str1 == str3 ? "Same\n" : "Different\n");
Different Same
-
使用
compare()
函数可以比较两个字符串的大小关系。string str1 = "ABCD"; string str2 = "ABCd"; printf("%s", str1.compare(str2) ? "Longer\n" : "Shorter\n");
Longer
子串查找(Substring Searching):
-
使用
find()
函数可以在字符串中查找指定子串的位置。string str = "Hello,World!"; string substr = "World"; cout << str.find(substr);
6
-
使用
rfind()
函数可以从字符串末尾开始查找指定子串的位置。string str = "Hello,World!"; string substr = "l"; cout << str.find(substr) << endl; cout << str.rfind(substr) << endl;
2 9
子串提取(Substring Extraction):
-
使用
substr(start,end) or substr(position)
函数([start,end)
or[positon,end)
)可以从字符串中提取指定位置和长度的子串,end
默认为字符串末尾后一位。string str = "Hello,World!"; string substr1 = str.substr(0, 5); string substr2 = str.substr(5); cout << substr1 << endl; cout << substr2 << endl;
Hello ,World!
字符串分割(String Splitting):
-
使用
find_first_of()
和substr()
函数可以实现字符串的分割。find_first_of()
是一个字符串成员函数,用于在字符串中查找第一个与给定字符序列中的任何字符匹配的字符的位置。string str = "Hello,World!"; int index = str.find_first_of("Wor"); cout << index << endl; string substr1 = str.substr(0,index); string substr2 = str.substr(index); cout << substr1 << endl; cout << substr2 << endl;
4 Hell o,World!
字符串替换(String Replacement):
-
使用``replace()`函数可以将字符串中的指定子串替换为另一个字符串。
replace()
函数有多个重载版本,根据不同的参数类型和个数,可以实现不同的字符串替换操作。以下是其中一些常用的重载版本:-
替换指定位置的子串:
std::string& replace(size_t pos, size_t len, const std::string& str);
string str = "Hello,World!"; cout << str << endl; string substr = "Wo"; string new_str = "wor"; str.replace(str.find(substr), new_str.length(), new_str);
Hello,World! Hello,world!
-
替换指定位置的子串,或插入/删除内容:
std::string& replace(size_t pos, size_t len, const std::string& str, size_t subpos, size_t sublen);
string str = "Hello,World!"; cout << str << endl; string substr = "Wor"; string new_str = "wor"; str.replace(str.find(substr), substr.length(), new_str, 0, new_str.length()); cout << str << endl;
Hello,World! Hello,world!
-
替换指定位置的子串,或插入/删除内容(C风格字符串作为参数)
std::string& replace(size_t pos, size_t len, const char* s);
-
替换指定位置的子串,或插入/删除内容(C风格字符串作为参数)
std::string& replace(size_t pos, size_t len, const char* s, size_t n);
-
字符串大小写转换(Case Conversion):
-
使用
tolower()
函数可以将字符串转换为小写形式。string ToLower(string str){ string temp = str; for (auto &ch : temp) { ch = tolower(ch); } return temp; } int main() { string str = "Hello,World!"; string newstr = ToLower(str); cout << str << endl; cout << newstr << endl; return 0; }
Hello,World! hello,world!
-
使用
toupper()
函数可以将字符串转换为大写形式。string ToUpper(string str){ string temp = str; for (auto &ch : temp) { ch = toupper(ch); } return temp; } int main() { string str = "Hello,World!"; string newstr = ToUpper(str); cout << str << endl; cout << newstr << endl; return 0; }
Hello,World! HELLO,WORLD!
字符串与数字的相互转换:
-
使用
stoi()
函数可以将字符串转换为整数。string str_num = "12345"; int num = stoi(str_num); cout << num;
12345
-
使用
stof()
函数可以将字符串转换为浮点数。string str_num = "12345.33"; double num = stof(str_num); cout << setprecision(8) <<num;
12345.33
-
使用
to_string()
函数可以将数字转换为字符串。int num = 10; double fnum = 3.14; cout << to_string(num) << endl; cout << to_string(fnum) << endl;
10 3.140000