文章目录
- 1. 定义和初始化字符串
- 2. 字符串的基本操作
- 2.1 获取字符串长度
- 2.2 检查字符串是否为空
- 2.3 访问字符串中的字符
- 3. 输入字符串
- 4. 常用的字符串操作
- 4.1 截取子字符串
- 4.2 查找子字符串
- 4.3 替换字符串
- 4.4 插入字符串
- 4.5 删除字符串
- 5. 字符串的排序
- 6. 字符串与数值的转换
- 6.1 字符串转数值
- 6.2 数值转字符串
- 7. C++新特性补充
- 7.1 使用string_view(C++17)
- 7.2 格式化字符串(C++20)
在C++中,字符串是由一系列字符组成的序列:
- C风格:使用字符数组表示,以
\0
结尾。 - C++字符串类:使用
std::string
类,更强更方便。
这里主要介绍std::string
的使用。
1. 定义和初始化字符串
#include <iostream>
#include <string> // 必须包含这个头文件
int main() {
// 1. 定义一个空字符串
std::string s1;
// 2. 直接初始化
std::string s2 = "Hello"; // 使用等号初始化
std::string s3("World"); // 使用括号初始化
// 3. 重复字符初始化
std::string s4(5, 'A'); // 结果是 "AAAAA"
// 4. 从另一个字符串的子串初始化
std::string s5(s2, 1, 3); // 从s2的第1个字符开始,取3个字符,结果是 "ell"
std::cout << s2 << " " << s3 << std::endl; // 输出: Hello World
return 0;
}
2. 字符串的基本操作
2.1 获取字符串长度
size_t length() const noexcept;
size_t size() const noexcept;
- 参数:无
- 返回值:返回字符串的长度(以字符数为单位)。
std::string s = "Hello";
int len1 = s.length(); // 结果是 5
int len2 = s.size(); // 结果也是 5
2.2 检查字符串是否为空
bool empty() const noexcept;
- 参数:无
- 返回值:如果字符串为空,返回
true
;否则返回false
。
if (s.empty()) {
std::cout << "空" << std::endl;
} else {
std::cout << "非空" << std::endl;
}
2.3 访问字符串中的字符
char& operator[](size_t pos);
char at(size_t pos) const;
-
参数:
pos
:要访问的字符位置(从0开始)。 -
返回值:
operator[]
:返回指定位置的字符引用。at
:返回指定位置的字符,支持边界检查,越界会抛出std::out_of_range
异常。
char firstChar = s[0]; // 获取第一个字符,结果是 'H'
char secondChar = s.at(1); // 获取第二个字符,结果是 'e'
3. 输入字符串
#include <iostream>
#include <string>
int main() {
// 1. 使用cin(遇到空格或换行结束)
std::string s1;
std::cin >> s1; // 输入 "Hello World",s1只会得到 "Hello"
// 2. 使用getline读取整行
std::string s2;
std::getline(std::cin, s2); // 输入 "Hello World",s2会得到完整的 "Hello World"
// 3. 使用字符数组(C风格)
char buf[100];
std::cin.getline(buf, sizeof(buf)); // 读取一行到字符数组中
std::cout << s1 << std::endl;
std::cout << s2 << std::endl;
return 0;
}
4. 常用的字符串操作
4.1 截取子字符串
std::string substr(size_t pos = 0, size_t len = npos) const;
- 参数:
pos
:子字符串的起始位置。len
:子字符串的长度,默认为直到字符串末尾。 - 返回值:返回一个新的字符串,表示截取的子字符串。
std::string s = "Hello World";
std::string sub = s.substr(6, 5);
// 从第6个字符开始,截取5个字符,结果是 "World"
4.2 查找子字符串
size_t find(const std::string& str, size_t pos = 0) const noexcept;
- 参数:
str
:要查找的子字符串。pos
:从哪个位置开始查找,默认为0。 - 返回值:如果找到,返回子字符串的起始位置;否则返回
std::string::npos
。
size_t pos = s.find("World"); // 查找 "World" 的位置,结果是 6
if (pos != std::string::npos) {
std::cout << "找到了!位置是:" << pos << std::endl;
} else {
std::cout << "没找到!" << std::endl;
}
4.3 替换字符串
std::string& replace(size_t pos, size_t len, const std::string& str);
- 参数:
pos
:替换起始位置。len
:替换的字符长度。str
:用于替换的新字符串。 - 返回值:返回修改后的字符串引用。
s.replace(6, 5, "abcdefgh"); // 从第6个字符开始,替换5个字符为 "abcdefgh"
std::cout << s << std::endl; // 输出: Hello abcdefgh
4.4 插入字符串
std::string& insert(size_t pos, const std::string& str);
- 参数:
pos
:插入位置。str
:要插入的字符串。 - 返回值:返回修改后的字符串引用。
s.insert(5, " Beautiful"); // 在第5个字符后插入 " Beautiful"
std::cout << s << std::endl; // 输出: Hello Beautiful Universe
4.5 删除字符串
std::string& erase(size_t pos = 0, size_t len = npos);
- 参数:
pos
:删除起始位置。len
:删除的字符长度,默认为直到字符串末尾。 - 返回值:返回修改后的字符串引用。
s.erase(5, 10); // 从第5个字符开始,删除10个字符
std::cout << s << std::endl; // 输出: Hello Universe
5. 字符串的排序
template< class RandomIt >
void sort( RandomIt first, RandomIt last );
- 参数:
first
:排序范围的起始迭代器。last
:排序范围的结束迭代器。 - 返回值:无。
#include <algorithm>
std::string s = "hello";
std::sort(s.begin(), s.end());
std::cout << s << std::endl; // 输出: ehllo
6. 字符串与数值的转换
6.1 字符串转数值
int stoi(const std::string& str, size_t* idx = nullptr, int base = 10);
double stod(const std::string& str, size_t* idx = nullptr);
- 参数:
str
:要转换的字符串。idx
:可选参数,指向转换结束的位置。base
:进制,默认为10。
- 返回值:返回转换后的数值。
std::string numStr = "123";
int num = std::stoi(numStr); // 字符串转整数
double d = std::stod("3.14"); // 字符串转浮点数
6.2 数值转字符串
std::string to_string(int val);
std::string to_string(double val);
- 参数:
val
:要转换的数值。 - 返回值:返回转换后的字符串。
int num = 42;
std::string str = std::to_string(num); // 整数转字符串
std::cout << str << std::endl; // 输出: 42
7. C++新特性补充
7.1 使用string_view(C++17)
class std::string_view {
public:
std::string_view(const std::string& str);
std::string_view(const char* str);
};
- 参数:
str
:字符串或字符数组。 - 返回值:构造一个轻量级的字符串视图对象。
#include <string_view>
void print(std::string_view sv) {
std::cout << sv << std::endl;
}
int main() {
std::string s = "Hello World";
print(s); // 输出: Hello World
print("C++ is fun"); // 输出: C++ is fun
return 0;
}
7.2 格式化字符串(C++20)
template<class... Args>
std::string format(const std::string& fmt, const Args&... args);
- 参数:
fmt
:格式化字符串模板。args
:格式化参数。 - 返回值:返回格式化后的字符串。
#include <format>
std::string message = std::format("Hello, {}!", "World");
std::cout << message << std::endl; // 输出: Hello, World!