C++ 字符串
最后修改于 2023 年 1 月 9 日
C++ 字符串教程演示了如何在 C++ 中处理字符串。
字符串是字符序列。C++ 使用 std::string 类型来表示字符串。
字符串字面量中的字符必须用双引号括起来。
C++ 字符串访问字符
要访问字符串的字符,我们可以使用 [] 运算符或 at 方法。此外,front 方法访问第一个字符,back 方法访问最后一个字符。
#include <iostream>
using std::cout;
using std::endl;
using std::string;
int main() {
string msg = "an old falcon";
cout << msg.at(4) << endl;
cout << msg[5] << endl;
cout << msg.front() << endl;
cout << msg.back() << endl;
return 0;
}
在示例中,我们访问字符串的字符。
$ ./access l d a
C++ 字符串连接
+ 运算符用于连接字符串。
#include <iostream>
using std::string;
using std::cin;
using std::cout;
using std::endl;
int main() {
string name, msg;
cout << "Enter your name: ";
getline(cin, name);
msg = "Hello " + name + "!";
cout << msg << endl;
return 0;
}
使用 getline 方法,我们可以从用户那里读取输入,并将其与其他字符串连接起来形成一个消息。
$ ./concat Enter your name: Jan Hello Jan!
C++ 字符串转整数
stoi 函数将字符串转换为有符号整数。
#include <iostream>
#include <string>
using std::string;
using std::cout;
using std::endl;
using std::stoi;
int main () {
string str1 = "12";
string str2 = "18.97";
string str3 = "4 foxes";
int val1 = stoi(str1);
int val2 = stoi(str2);
int val3 = stoi(str3);
cout << val1 << endl;
cout << val2 << endl;
cout << val3 << endl;
return 0;
}
我们将三个字符串转换为整数。
$ ./str2int 12 18 4
C++ 字符串修改
C++ 有几种修改字符串的方法。
#include <iostream>
using std::string;
using std::cout;
using std::endl;
int main() {
string msg = "an old";
msg.append(" falcon");
cout << msg << endl;
msg.push_back('.');
cout << msg << endl;
msg.pop_back();
cout << msg << endl;
msg.erase(0, 3);
cout << msg << endl;
msg.insert(4, "gray ");
cout << msg << endl;
msg.replace(9, 6, "eagle");
cout << msg << endl;
msg.clear();
cout << msg.size() << endl;
return 0;
}
在示例中,使用 append、push_back、pop_back、erase、insert、replace 和 clear 方法修改了初始字符串。
$ ./modify an old falcon an old falcon. an old falcon old falcon old gray falcon old gray eagle 0
C++ 字符串比较
使用 compare 方法比较字符串。
#include <iostream>
using std::string;
using std::cout;
using std::endl;
int main() {
string word1 = "blue";
string word2 = "blues";
if (word1.compare(word2) == 0) {
cout << "words are equal" << endl;
} else {
cout << "words are not equal" << endl;
}
if (word1.compare(0, 4, word2, 0, 4) == 0) {
cout << "words are equal" << endl;
} else {
cout << "words are not equal" << endl;
}
return 0;
}
我们比较了两个单词。在第二种情况下,我们指定了要比较的字符范围。
$ ./comparing words are not equal words are equal
C++ 子字符串
substr 函数返回一个子字符串。
#include <iostream>
#include <string>
using std::string;
using std::cout;
using std::endl;
int main() {
string word = "an old falcon";
cout << word.substr(0, 2) << endl;
cout << word.substr(3, 3) << endl;
cout << word.substr(7, 6) << endl;
return 0;
}
我们获取了初始字符串的三个子字符串。
$ ./substring an old falcon
C++ 字符串循环
我们可以使用 while 和 for 循环遍历字符串。
#include <iostream>
using std::cout;
using std::endl;
using std::string;
int main() {
string msg = "an old falcon";
int i = 0;
while (i < msg.size()) {
cout << msg[i] << " ";
i++;
}
cout << endl;
for (const auto &c : msg) {
cout << c << " ";
}
cout << endl;
for (auto it = msg.begin(); it != msg.end(); it++) {
cout << *it << " ";
}
cout << endl;
for (string::size_type i = 0; i < msg.size(); i++) {
std::cout << msg.at(i) << " ";
}
cout << endl;
return 0;
}
我们遍历字符串并打印其字符。我们使用了经典的 while 和 for 循环以及 for-range 循环。
$ ./looping a n o l d f a l c o n a n o l d f a l c o n a n o l d f a l c o n a n o l d f a l c o n
C++ 字符串查找/反向查找
find 搜索字符串中指定字符串的第一次出现,而 rfind 搜索最后一次出现。
#include <iostream>
using std::string;
using std::cout;
using std::endl;
int main() {
string text = "I saw a red fox yesterday; a red old fox.";
int pos1 = text.find("fox");
int pos2 = text.rfind("fox");
int pos3 = text.find("fox", 15);
cout << pos1 << endl;
cout << pos2 << endl;
cout << pos3 << endl;
return 0;
}
我们使用 find 和 rfind 方法来查找“fox”字符串。这些方法返回字符位置的索引。
int pos3 = text.find("fox", 15);
重载的 find 方法指定搜索的起始位置。
$ ./finding 12 37 37
C++ 读取文件
在下面的示例中,我们读取一个文本文件。
wind sky blue water falcon rock wood cup cloud war
文件里有一系列单词。
#include <iostream>
#include <fstream>
#include <string>
using std::string;
using std::cout;
using std::cerr;
using std::endl;
using std::getline;
using std::ifstream;
int main() {
ifstream filename("words.txt");
if (filename.is_open()) {
string line;
while (getline(filename, line)) {
cout << line << endl;
}
filename.close();
} else {
cerr << "Unable to open file";
}
return 0;
}
我们逐行读取文件,并将每一行打印到控制台。
$ ./read_file wind sky blue water falcon rock wood cup cloud war
C++ 字符串 starts_with
starts_with 方法检查字符串是否以给定的前缀开头。该方法已包含在 C++20 中。
#include <iostream>
#include <fstream>
using std::string;
using std::cout;
using std::cerr;
using std::endl;
using std::getline;
using std::ifstream;
int main() {
ifstream filename("words.txt");
if (filename.is_open()) {
string line;
while (getline(filename, line)) {
if (line.starts_with('w')) {
cout << line << endl;
}
}
filename.close();
} else {
cerr << "Unable to open file";
}
return 0;
}
我们从 words.txt 文件中读取单词,并打印那些以“w”开头的单词。
$ ./starts_with wind water wood war
C++ 字符串 ends_with
ends_with 方法检查字符串是否以给定的后缀结尾。该方法已包含在 C++20 中。
#include <iostream>
#include <sstream>
using std::string;
using std::cout;
using std::endl;
using std::getline;
using std::istringstream;
int main() {
string words = "wind\nsky\blue\nwater\nfalcon\nrock\nwood\ncup\ncloud\nwar";
istringstream data(words);
for (string line; getline(data, line);) {
if (line.ends_with('d')) {
cout << line << endl;
}
}
return 0;
}
我们有一个由换行符分隔的大量单词组成的字符串。我们将字符串转换为流,并使用 getline 读取单词。我们打印那些以“d”结尾的单词。
$ ./ends_with wind wood cloud
在本文中,我们处理了 C++ 中的字符串。