C++基础学习(六)字符串

前言:C++中<string>头文件定义了string类型,让我们操作字符串更加方便和灵活,这里只介绍几种常用的string字符串操作 方法。

(一)定义字符串

#include<iostream>
#include<string>     //要使用string就得先引入string标准模板类

using namespace std;

void main()
{
	//字符串的定义,使用string定义的变量值都必须使用双引号
	string myStr1 = "Sometimes you play basketball on the ground";   //直接声明一段字符串
	string empty1;            //可以不初始化
	string empty2 = "";	      //初始化为空字符串

	string myStr2 = myStr1;   //使用已声明的字符串变量进行赋值

	string myStr3{ myStr1, 0, 8 };   //可以提取已有的字符串,该字符串变量为myStr1中的前8个字符
	cout << myStr3 << endl;          //应该输出Sometime
}

(二)字符串合并和子字符串

#include<iostream>
#include<string>     //要使用string就得先引入string标准模板类

using namespace std;

void main()
{
	//用string定义出来的字符串已经是一个对象了,我们可以操作它
	string myStr1 = "You";
	string myStr2 = " are a student.";

	int strLength = myStr1.length();    //可以获取该字符串的长度
	string newStr = myStr1 + myStr2;    //可以直接用+运算符来连接字符串
	//myStr1 += myStr2;     //这种方式直接在myStr1后进行连接,myStr1会成为合并后的字符串

	cout << "myStr1的长度为:" << strLength << " 合并后的字符串为:" << newStr << endl;

	//处理字符串和处理数组很相似,毕竟string从是对字符数组的延伸和升级
	for (int i = 0; i < newStr.length(); i++)
	{
		cout << newStr[i] << " ";    //通过索引获取对应的字符
	}
	cout << endl;

	//访问子字符串
	string myStr3 = newStr.substr(10, 2);      //访问的是从位置10开始向后2个字符
	string myStr4 = newStr.substr(10);         //会从位置10开始访问之后所有的字符
	cout << myStr3 << "  " << myStr4 << endl;  //输出st  student
}

(三)比较字符串

#include<iostream>
#include<string>     //要使用string就得先引入string标准模板类

using namespace std;

void main()
{
	//比较字符串,C++进行字符串的比较时,是逐个比较其中的字符,由于比较的是该字符对应的字符编码,所以这种比较是区分大小写的
	string word1 = "Some";      //可以测试不同的字符串,当然中文字符串也是可以的
	string word2 = "SoSo";

	if (word1 < word2)     //使用if语句,由于只需写一条语句,所以可以不写{}
		cout << word1 << " < " << word2 << endl;
	else if (word1 == word2)
		cout << word1 << " = " << word2 << endl;
	else
		cout << word1 << " > " << word2 << endl;

	//不过string为我们提供了相应的函数用于比较字符串
	int result1 = word1.compare(word2);

	cout << result1 << endl;      //该函数的返回值为0,当word1大于word2时,result1 = 1;
	//当word1等于word2时,result1 = 0
	//当word1小于于word2时,result1 = -1

	//当然这个函数不会这么简单,他还可以比较指定索引位置的字符
	int result2 = word1.compare(0, 2, word2);   //word1的从0索引开始的长度为2的子字符串与word2整体进行比较,并返回结果
	cout << result2 << endl;

	int result3 = word1.compare(0, 2, word2, 2, 2); //word1的从0索引开始的长度为2的子字符串与
	//word2的从2索引开始的长度为2的子字符串进行比较,并返回结果
	cout << result3 << endl;
}

当然你也可以用substr()获取子字符串去比较,这两种方法都可以。


(四)搜索字符串

#include<iostream>
#include<string>     //要使用string就得先引入string标准模板类

using namespace std;

void main()
{
	//搜索字符串
	string myStr = "Now we learn how to find a string.";
	int postion1 = myStr.find("we");    //改函数返回搜索到的第一个字符串中第一个字符的索引位置
	cout << postion1 << endl;           //myStr中we中的w在索引4的位置

	//也可以给find()设定一些搜索条件
	myStr.find("in", 10);   //以索引10为起点向后搜索
}

string还提供了其他一些搜索方法,如find_first_of()、find_last_of()、rfind()等等,这些都是为了让你更精确和更灵活地搜索想要的字符串。


(五)修改字符串

#include<iostream>
#include<string>     //要使用string就得先引入string标准模板类

using namespace std;

void main()
{	
	//修改字符串
	//插入字符串
	string myStr = "Now we learn how to change a string.";
	string word1 = "Oh! Boys! ";
	myStr.insert(0, word1);    //在myStr索引0的前面插入字符串,第一个参数就是你想插入的位置
	cout << myStr << endl;

	//当然还有更精细的操作
	string word2 = "Today is Friday ";
	word1.insert(3, word2, 5, 11);       //在word1索引3的位置前面 插入word2的从索引5开始向后长度为11的子字符串
	cout << word1 << endl;

	//替换字符串
	word2.replace(6, 3, "I don't know");   //将word2从索引6位置开始往后长度为3的子字符串替换成我们指定的字符串
	cout << word2 << endl;

	//删除字符串
	word1.replace(0, word1.length(), "");   //将word1中所有的字符串都替换成空字符串
	word2.clear();        //不过还有更简便的方法
	cout << word1.length() << " " << word2.length() << endl;	
}
向string中的方法传递的参数含义大都类似,如你一般需要传递字符串索引,所需字符串的长度等,虽然这些方法的使用方式很多,但只要抓住几个关键的参数就不会混乱了。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值