C++ 字符串(string)常用操作总结
C++ 字符串(string)常用操作总结
由单引号括起来的一个字符被称作
如字面值 ‘A’ 表示的就是单独字符
0、常用功能汇总
s.insert(pos, args) | 在 |
---|---|
s.erase(pos, len) | 删除从 |
s.assign(args) | 将 |
s.append(args) | 将 |
s.replace(range, args) | 将 |
s.find(args) | 查找 |
s.rfind(args) | 查找 |
to_string(val) | 将数值 |
stoi(s) / atoi(c) | 字符串 |
stof(s) / atof(s) | 字符串 |
s.substr(pos, n) | 从索引 |
reverse(s2.begin(), s2.end()) | 反转 |
1、定义一个字符串
使用标准库类型
string s1; // 初始化一个空字符串
string s2 = s1; // 初始化s2,并用s1初始化
string s3(s2); // 作用同上
string s4 = "hello world"; // 用 "hello world" 初始化 s4,除了最后的空字符外其他都拷贝到s4中
string s5("hello world"); // 作用同上
string s6(6,'a'); // 初始化s6为:aaaaaa
string s7(s6, 3); // s7 是从 s6 的下标 3 开始的字符拷贝
string s8(s6, pos, len); // s7 是从 s6 的下标 pos 开始的 len 个字符的拷贝
使用
2、读写string 操作
输入时遇到空格或回车键将停止。但需要注意的是只有按下回车键时才会结束输入执行,当按下空格后还能继续输入,但最终存到字符串中的只是第一个空格之前输入的字符串(开头的空白除外,程序会自动忽略开头的空白的
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
string s1, s2, s3; // 初始化一个空字符串
// 单字符串输入,读入字符串,遇到空格或回车停止
cin >> s1;
// 多字符串的输入,遇到空格代表当前字符串赋值完成,转到下个字符串赋值,回车停止
cin >> s2 >> s3;
// 输出字符串
cout << s1 << endl;
cout << s2 << endl;
cout << s3 << endl;
return 0;
}
// 运行结果 //
abc def hig
abc
def
hig
如果希望在最终读入的字符串中保留空格,可以使用
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
string s1 ; // 初始化一个空字符串
getline(cin , s1);
cout << s1 << endl; // 输出
return 0;
}
// 结果输出 //
abc def hi
abc def hi
3、查询字符串信息、索引
可以用
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
string s1 = "abc"; // 初始化一个字符串
cout << s1.empty() << endl; // s 为空返回 true,否则返回 false
cout << s1.size() << endl; // 返回 s 中字符个数,不包含空字符
cout << s1.length() << endl; // 作用同上
cout << s1[1] << endl; // 字符串本质是字符数组
cout << s1[3] << endl; // 空字符还是存在的
return 0;
}
// 运行结果 //
0
3
3
b
4、拼接、比较等操作
s1+s2 // 返回 s1 和 s2 拼接后的结果。加号两边至少有一个 string 对象,不能都是字面值
s1 == s2 // 如果 s1 和 s2 中的元素完全相等则它们相等,区分大小写
s1 != s2
<, <=, >, >= // 利用字符的字典序进行比较,区分大小写
5、cctype 头文件( 判断字符类型:大/ 小写字母、标点、数字等)
isalnum(c) // 当是字母或数字时为真
isalpha(c) // 当是字母时为真
isdigit(c) // 当是数字是为真
islower(c) // 当是小写字母时为真
isupper(c) // 当是大写字母时为真
isspace(c) // 当是空白(空格、回车、换行、制表符等)时为真
isxdigit(c) // 当是16进制数字是为真
ispunct(c) // 当是标点符号时为真(即c不是 控制字符、数字、字母、可打印空白 中的一种)
isprint(c) // 当时可打印字符时为真(即c是空格或具有可见形式)
isgraph(c) // 当不是空格但可打印时为真
iscntrl(c) // 当是控制字符时为真
tolower(c) // 若c是大写字母,转换为小写输出,否则原样输出
toupper(c) // 类似上面的
6、for 循环遍历
可以使用
(如果想要改变
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main(void)
{
string s1 = "nice to meet you~"; // 初始化一个空字符串
// 如果想要改变 string 对象中的值,必须把循环变量定义为引用类型。引用只是个别名,相当于对原始数据进行操作
for(auto &c : s1)
c = toupper(c);
cout << s1 << endl; // 输出
return 0;
}
// 运行结果 //
NICE TO MEET YOU~
7、修改string 的操作
在
s.insert(pos, args)
// 在 s 的位置 0 之前插入 s2 的拷贝
s.insert(0, s2)
删除从
s.erase(pos, len)
将
s.assign(args)
将
s.append(args)
将
s.replace(range, args)
// 从位置 3 开始,删除 6 个字符,并插入 "aaa".删除插入的字符数量不必相等
s.replace(3, 6, "aaa")
8、string 搜索操作
搜索操作返回指定字符出现的下标,如果未找到返回
s.find(args) // 查找 s 中 args 第一次出现的位置
s.rfind(args) // 查找 s 中 args 最后一次出现的位置
在
s.find_first_of(args) // 在 s 中查找 args 中任何一个字符最早出现的位置
s.find_last_of(args) // 在 s 中查找 args 中任何一个字符最晚出现的位置
例如:
string s1 = "nice to meet you~";
cout << s1.find_first_of("mey") << endl; // 输出结果为 3,'e' 出现的最早
在
s.find_first_not_of(args) // 查找 s 中 第一个不在 args 中的字符的位置
s.find_last_not_of(args) // 查找 s 中 最后一个不在 args 中的字符的位置
例如:
string s1 = "nice to meet you~";
cout << s1.find_first_not_of("nop") << endl; // 输出结果为 1 ,'i' 不在 "nop" 里
9、string、char 型与数值的转换
**1、
string s = to_string(val)
2、转换为整数并返回。返回类型分别是
stoi(s)
// 函数原型 int stoi (const string& str, size_t* idx = 0, int base = 10);
stoi(s, p, b)
stol(s, p, b)
stoul(s, p, b)
stoll(s, p, b)
stoull(s, p, b)
// 例如
string s1 = "11"; // 初始化一个空字符串
int a1 = stoi(s1);
cout << a1 << endl; // 输出 11
int a2 = stoi(s1, nullptr, 8);
cout << a2 << endl; // 输出 9
int a3 = stoi(s1, nullptr, 2);
cout << a3 << endl; // 输出 3
**3、转换为浮点数并返回。
stof(s)
stof(s, p)
stod(s, p)
stold(s, p)
**4、
atoi(c)
// 函数原型 int atoi(const char *_Str)
atol(c)
atoll(c)
atof(c)
10、字符串反转
使用
string s2 = "12345"; // 初始化一个字符串
reverse(s2.begin(), s2.end()); // 反转 string 定义的字符串 s2
cout << s2 << endl; // 输出 54321
11、提取字串
使用
inline std::__cxx11::string std::__cxx11::string::substr(std::size_t __pos, std::size_t __n) const