1.1.1 string与char
- 当需要存储和处理单个字符时,通常使用
char
。 - 当需要存储和处理一个或多个字符串时,通常使用
string
类。
string与char的声明与定义:
string a="xxxx";
char a='b';
注意:
-
双引号(""):
- 双引号主要用于表示字符串。在C++中,字符串是由一系列字符组成的,以空字符'\0'结束。双引号用于定义一个字符串,例如"Hello, World!"。
- 双引号内可以包含转义字符,例如'\n'表示换行,'\t'表示制表符等。
-
单引号('):
- 单引号主要用于表示字符。例如,'a'表示字符a,'B'表示字符B,以此类推。
- 在单引号内,不能包含转义字符。如果尝试在单引号内使用转义字符,例如'\n'或'\t',编译器会报错。
1.1.2 string的一些常用函数
- 构造函数:用于创建字符串。例如,
std::string a("Hello");
- 连接:使用
+
运算符或者append
函数可以连接字符串。 - 获取长度:使用
size()
或length()
函数可以得到字符串的长度a.size()
。 - 查找:可以使用
find()
函数查找子串或字符的位置。 - 替换:
replace()
函数可以替换字符串中的子串或字符。 - 比较:使用
==
、!=
、<
、<=
、>
、>=
运算符或者compare()
函数可以比较两个字符串。 - 子串:可以使用
substr()
函数获取字符串的子串。 - 插入:
insert()
函数可以在字符串的特定位置插入子串。 - 删除:
erase()
函数可以删除字符串中的子串。 - 遍历:可以使用下标
[]
或者at()
函数访问字符串中的单个字符。 - 字符串转义:可以使用
replace()
函数对特定字符进行转义处理。 - 查找第一个非空字符:可以使用
find_first_not_of()
函数查找第一个非空字符的位置。 - 查找最后一个非空字符:可以使用
find_last_not_of()
函数查找最后一个非空字符的位置。 - 反转字符串:可以使用
reverse()
函数反转字符串。 - 替换所有出现的子串:可以使用
replace_all()
函数替换所有出现的特定子串。 - 将数字转换为字符串:可以使用
to_string()
函数将数字转换为字符串。 - 判断是否以某个字符串开头或结尾:可以使用
starts_with()
或ends_with()
函数判断字符串是否以特定字符串开头或结尾。
#include <iostream>
#include <string>
int main() {
// 构造函数
std::string str1("Hello");
std::cout << str1 << std::endl;
// 连接
std::string str2 = "World";
std::string str3 = str1 + str2; // "HelloWorld"
std::cout << str3 << std::endl;
// 获取长度
size_t len = str3.size();
std::cout << "Length of string: " << len << std::endl;
// 查找子串位置
size_t pos = str3.find("World");
std::cout << "Position of \"World\": " << pos << std::endl;
// 替换子串
str3.replace(pos, 5, "Universe"); // "HelloUniverse"
std::cout << str3 << std::endl;
// 比较字符串
std::string str4 = "Hello";
bool isEqual = (str3 == str4);
std::cout << "Is str3 equal to str4? " << isEqual << std::endl;
// 子串
std::string subStr = str3.substr(0, 5); // "Hello"
std::cout << subStr << std::endl;
// 插入字符串
std::string str5 = " in ";
str3.insert(6, str5); // "Hello in World"
std::cout << str3 << std::endl;
// 删除子串
str3.erase(0, 5); // "World"
std::cout << str3 << std::endl;
// 遍历字符串
for(char c : str3) {
std::cout << c << " ";
}
std::cout << std::endl;
// 字符串转义
std::string str6 = "Hello\n"; // 新行字符转义为换行符
std::cout << str6 << std::endl;
// 查找第一个非空字符位置和最后一个非空字符位置
size_t firstNonBlankPos = str6.find_first_not_of(" \t\n"); // 忽略空格、制表符和换行符
size_t lastNonBlankPos = str6.find_last_not_of(" \t\n"); // 忽略空格、制表符和换行符
std::cout << "First non-blank position: " << firstNonBlankPos << std::endl;
std::cout << "Last non-blank position: " << lastNonBlankPos << std::endl;
// 反转字符串
std::string str = "Hello World";
std::reverse(str.begin(), str.end());
std::cout << str << std::endl; // 输出:dlroW olleH
// 分割字符串
std::string str2 = "apple,banana,orange";
std::vector<std::string> fruits = std::sregex_token_iterator(str2.begin(), str2.end(), ",");
for (const auto& fruit : fruits) {
std::cout << fruit << std::endl;
}
// 输出:apple
// 输出:banana
// 输出:orange
// 替换所有出现的子串
std::string str3 = "Hello World";
std::replace_all(str3, "World", "Universe");
std::cout << str3 << std::endl; // 输出:Hello Universe
// 将数字转换为字符串
int num = 123;
std::string str4 = std::to_string(num);
std::cout << str4 << std::endl; // 输出:123
// 判断是否以某个字符串开头或结尾
std::string str5 = "Hello World";
bool startsWith = str5.starts_with("Hello");
bool endsWith = str5.endsWith("World");
std::cout << std::boolalpha; // 输出:true
std::cout << startsWith << std::endl; // 输出:true
std::cout << endsWith << std::endl; // 输出:true
return 0;
}