string的详细用法c++
std::string
是 C++ 中用于处理字符串的类,提供了丰富的成员函数和操作符来方便地操作字符串。以下是 std::string
的详细用法及示例:
1. 基本用法
1.1 创建和初始化
#include <iostream>
#include <string>
int main() {
// 默认初始化
std::string s1;
// 用C风格字符串初始化
std::string s2 = "Hello";
// 用另一个std::string初始化
std::string s3(s2);
// 用字符重复初始化
std::string s4(5, 'A'); // "AAAAA"
std::cout << "s1: " << s1 << std::endl; // 空字符串
std::cout << "s2: " << s2 << std::endl; // Hello
std::cout << "s3: " << s3 << std::endl; // Hello
std::cout << "s4: " << s4 << std::endl; // AAAAA
return 0;
}
1.2 访问字符
可以通过下标或 at()
方法访问字符串中的字符。
#include <iostream>
#include <string>
int main() {
std::string s = "Hello";
// 使用下标访问
std::cout << "First character: " << s[0] << std::endl; // H
// 使用at()访问
std::cout << "Second character: " << s.at(1) << std::endl; // e
// 修改字符
s[0] = 'h';
std::cout << "Modified string: " << s << std::endl; // hello
return 0;
}
1.3 字符串长度
使用 size()
或 length()
获取字符串的长度。
#include <iostream>
#include <string>
int main() {
std::string s = "Hello, World!";
std::cout << "Length: " << s.length() << std::endl; // 13
std::cout << "Size: " << s.size() << std::endl; // 13
return 0;
}
1.4 字符串拼接
使用 +
或 append()
拼接字符串。
#include <iostream>
#include <string>
int main() {
std::string s1 = "Hello";
std::string s2 = "World";
// 使用 + 拼接
std::string s3 = s1 + ", " + s2 + "!";
std::cout << s3 << std::endl; // Hello, World!
// 使用 append() 拼接
s1.append(", ").append(s2).append("!");
std::cout << s1 << std::endl; // Hello, World!
return 0;
}
1.5 字符串比较
使用 ==
, !=
, <
, >
, <=
, >=
或 compare()
比较字符串。
#include <iostream>
#include <string>
int main() {
std::string s1 = "apple";
std::string s2 = "banana";
if (s1 == s2) {
std::cout << "s1 == s2" << std::endl;
} else if (s1 < s2) {
std::cout << "s1 < s2" << std::endl; // 输出
} else {
std::cout << "s1 > s2" << std::endl;
}
// 使用 compare()
int result = s1.compare(s2);
if (result == 0) {
std::cout << "s1 == s2" << std::endl;
} else if (result < 0) {
std::cout << "s1 < s2" << std::endl; // 输出
} else {
std::cout << "s1 > s2" << std::endl;
}
return 0;
}
2. 常用成员函数
2.1 substr()
提取子字符串。
#include <iostream>
#include <string>
int main() {
std::string s = "Hello, World!";
// 提取从索引7开始的5个字符
std::string sub = s.substr(7, 5);
std::cout << "Substring: " << sub << std::endl; // World
return 0;
}
2.2 find()
查找子字符串或字符。
#include <iostream>
#include <string>
int main() {
std::string s = "Hello, World!";
// 查找子字符串
size_t pos = s.find("World");
if (pos != std::string::npos) {
std::cout << "Found at index: " << pos << std::endl; // 7
} else {
std::cout << "Not found!" << std::endl;
}
// 查找字符
pos = s.find('o');
if (pos != std::string::npos) {
std::cout << "Found 'o' at index: " << pos << std::endl; // 4
}
return 0;
}
2.3 replace()
替换子字符串。
#include <iostream>
#include <string>
int main() {
std::string s = "Hello, World!";
// 替换从索引7开始的5个字符
s.replace(7, 5, "C++");
std::cout << "After replace: " << s << std::endl; // Hello, C++!
return 0;
}
2.4 erase()
删除子字符串或字符。
#include <iostream>
#include <string>
int main() {
std::string s = "Hello, World!";
// 删除从索引5开始的6个字符
s.erase(5, 6);
std::cout << "After erase: " << s << std::endl; // Hello!
return 0;
}
2.5 insert()
插入字符串或字符。
#include <iostream>
#include <string>
int main() {
std::string s = "Hello, !";
// 在索引7处插入字符串
s.insert(7, "World");
std::cout << "After insert: " << s << std::endl; // Hello, World!
return 0;
}
2.6 clear()
清空字符串。
#include <iostream>
#include <string>
int main() {
std::string s = "Hello, World!";
s.clear();
std::cout << "After clear: " << s << std::endl; // 空字符串
return 0;
}
2.7 empty()
检查字符串是否为空。
#include <iostream>
#include <string>
int main() {
std::string s = "Hello";
if (s.empty()) {
std::cout << "String is empty!" << std::endl;
} else {
std::cout << "String is not empty!" << std::endl; // 输出
}
return 0;
}
3. 其他常用操作
3.1 遍历字符串
#include <iostream>
#include <string>
int main() {
std::string s = "Hello";
// 使用下标遍历
for (size_t i = 0; i < s.size(); i++) {
std::cout << s[i] << " "; // H e l l o
}
std::cout << std::endl;
// 使用范围for循环遍历
for (char c : s) {
std::cout << c << " "; // H e l l o
}
std::cout << std::endl;
return 0;
}
3.2 字符串与数字的转换
#include <iostream>
#include <string>
int main() {
// 字符串转整数
std::string s1 = "123";
int num = std::stoi(s1);
std::cout << "Number: " << num << std::endl; // 123
// 整数转字符串
int n = 456;
std::string s2 = std::to_string(n);
std::cout << "String: " << s2 << std::endl; // 456
return 0;
}