[详解-字符串] C++必知必会 字符串-string常用各种操作解析

在C++中,字符串是一种用于存储文本数据的数据类型,用于表示字符序列。C++提供了string类来处理字符串,它位于头文件<string>中。string类提供了丰富的字符串操作功能,包括创建、访问、修改、搜索、连接等。

以下是关于C++中字符串的一些常用操作和相应的代码示例:

1.包含头文件:

首先,使用需要引入头文件 <string>

#include <string>

2.定义字符串:

string str;

3.初始化字符串:

	string str = "Hello World!";
	//输出:Hello World!
	cout << str << endl;

4.获得字符串长度:

4.1使用length()函数:

	string str = "Hello World!";
	//输出:12
	cout << str.length() << endl;

4.2使用字符串的size()函数:

	string str = "Hello World!";
	//输出:12
	cout << str.size() << endl;

4.3使用C风格的字符串函数strlen()(需要包含cstring头文件):

	#include<cstring>
	const char* cstr = "Hello world!";
	//输出:12
	cout << strlen(cstr) << endl;

5.访问字符串的元素:

5.1通过索引访问字符串:

注意:索引的下标从0到size()-1

	string str = "abcd1234";
	//输出:abcd1234
	for (int i = 0; i < str.size(); i++) { //这里使用str.length()也是可以的
		cout << str[i];
	}
	cout << endl;

5.2通过迭代器访问字符串:

①直接使用auto替代繁琐的类型声明:(推荐)

	string str = "abcd1234";
	//输出:abcd1234
	for (auto it : str) {
		cout << it;
	}
	cout << endl;

②声明类型

	string str = "abcd1234";
	//输出:abcd1234
	for (string::iterator it = str.begin(); it != str.end(); it++) {
		cout << *it;
	}
	cout << endl;

③或者用auto代替string,效果也是一样的:

	string str = "abcd1234";
	//输出:abcd1234
	for (auto it = str.begin(); it != str.end(); it++) {
		cout << *it;
	}
	cout << endl;

6.向字符串中插入元素:

6.1向字符串指定位置插入元素:

	string str = "abcdefg";
	//向字符串索引为3的位置插入"1234"
	str.insert(3, "1234");
	//输出:abc1234defg
	cout << str << endl;

	//向字符串索引为1的位置插入"A"
	str.insert(1, "A");
	//输出:aAbc1234defg
	cout << str << endl;

6.2向字符串开头插入元素:

	string str = "abcdefg";
	//向字符串索引为0的位置插入"1234",就是在字符串的开头添加"1234"
	str.insert(0, "1234"); 
	//输出:1234abcdefg
	cout << str << endl;

等价于要插入的字符串元素直接在前方与该字符串连接:

	string str = "abcdefg";
	str = "1234" + str;
	//输出:1234abcdefg
	cout << str << endl;

6.3向字符串结尾插入元素:

	string str = "abcdefg";
	//向字符串索引为str.size()的位置插入"1234",就是在字符串的末尾添加"1234"
	str.insert(str.size(), "1234");
	//输出:abcdefg1234
	cout << str << endl;

等价于要插入的字符串元素直接在后方与该字符串连接:

	string str = "abcdefg";
	str = str + "1234"; //或者str += "1234";
	//输出:abcdefg1234
	cout << str << endl;

7.连接字符串:

7.1通过+连接字符串:

	string str = "abcdefg";
	str = str + "1234"; //或者str += "1234";
	//输出:abcdefg1234
	cout << str << endl;

7.2通过append() 函数连接字符串:

	string str = "abcdefg";
	str.append("1234");
	//输出:abcdefg1234
	cout << str << endl;

8.删除字符串中的元素:erase()函数

	string str = "0123456789";
	//从索引为2的位置开始(包括2),删除3个字符
	str.erase(2, 3);
	//输出:0156789
	cout << str << endl;

	string str = "0000123456789";
	//从索引为0的位置开始(包括0),删除3个字符
	str.erase(0, 3);
	//输出:0123456789
	cout << str << endl;

9.修改字符串中的元素:

9.1通过索引修改:

	string str = "0123456789";
	str[0] = 'a';
	str[1] = 'b';
	str[2] = 'c';
	//输出:abc3456789
	cout << str << endl;

9.2通过replace()函数替换/修改:

	string str = "0123456789";
	//替换str中从索引1(包括1)开始的3个字符为"abcde"
	str.replace(1, 3, "abcde");
	//输出:0abcde456789
	cout << str << endl;

10.查找字符串中的元素/查找子串:

10.1find()函数:

该函数从字符串的开头开始查找子串。

①如果能找到,返回子串第一个字符所在的索引:

	string str = "Hello, world!";
	// 查找子串的位置
	int pos = str.find("world"); 
	//输出:7
	cout << pos << endl;

	int pos1 = str.find("llo");
	//输出:2
	cout << pos1 << endl;

②如果找不到,返回-1,不会报错:

	string str = "Hello, world!";
	// 查找子串的位置
	int pos = str.find("abc"); 
	//输出:-1
	cout << pos << endl;

10.2rfind()函数:

该函数从字符串的末尾开始查找子串。

①如果能找到,返回子串第一个字符所在的索引:

	string str = "Hello, world!";
	// 查找子串的位置
	int pos = str.rfind("world"); 
	//输出:7
	cout << pos << endl;

	int pos1 = str.rfind("llo");
	//输出:2
	cout << pos1 << endl;

②如果找不到,返回-1,不会报错:

	string str = "Hello, world!";
	// 查找子串的位置
	int pos = str.rfind("abc"); 
	//输出:-1
	cout << pos << endl;

11.截取子串:substr()函数

	string str = "0123456789";
	//截取字符串从索引3开始的,长度为5的子串
    string sub = str.substr(3, 5); 
	//输出:34567
	cout << sub << endl;

12.判断字符串是否为空:empty()函数

	string str1 = "abc";
	//输出:0,表示str1非空
	cout << str1.empty() << endl;

	string str2 = "";
	//输出:1,表示str2为空串
	cout << str2.empty() << endl;

13.字符串的比较:

13.1通过比较运算符来比较:

	string str1, str2;
	str1 = "Hello";
	str2 = "Hello";
	//输出:1,表示两个字符串相等
	cout << (str1 == str2) << endl;

	str1 = "Hello";
	str2 = "World";
	//输出:1,表示表示str1小于str2
	cout << (str1 < str2) << endl;


	str1 = "abcd";
	str2 = "ABCD";
	//输出:1,表示str1大于str2
	cout << (str1 > str2) << endl;

13.2通过compare()函数来比较:

返回0表示相等,负数表示str1小于str2,正数表示str1大于str2

	string str1, str2;
	str1 = "Hello";
	str2 = "Hello";
	//输出:0,表示两个字符串相等
	cout << str1.compare(str2) << endl;

	str1 = "Hello";
	str2 = "World";
	//输出:-1,表示表示str1小于str2
	cout << str1.compare(str2) << endl;

	
	str1 = "abcd";
	str2 = "ABCD";
	//输出:1,表示str1大于str2
	cout << str1.compare(str2) << endl;

14.字符串转数字:stoi() / stof() / stod()函数

14.1字符串转整数:stoi() 函数

	string str = "123";
	int num = stoi(str);
	//输出:123
	cout << num << endl;

14.2字符串转浮点数:stof()函数

	float floatNum = stof("3.14"); 
	//输出:3.14
	cout << floatNum << endl;

14.3字符串转双精度浮点数:stod()函数

	double doubleNum = stod("6.022e23");
	//输出:6.022e+23
	cout << doubleNum << endl;

15.读取一行文本并赋值:getline()函数

string line;
// 从标准输入读取一行文本并赋值给line
getline(cin, line); 

16.字符串转换大小写:

16.1转换为大写:toupper()函数

	string str = "Hello, World!";
	for (char& ch : str) {
		ch = toupper(ch); // 将字符串中的字符转换为大写形式
	}
	//输出:HELLO, WORLD!
	cout << str << endl;

16.2转换为小写:tolower()函数

	string str = "HELLO, WORLD!";
	for (char& ch : str) {
		ch = tolower(ch); // 将字符串中的字符转换为大写形式
	}
	//输出:hello, world!
	cout << str << endl;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值