字符串变量String的常用操作

#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main()
{
	string str1("helloxihuhellozhejianghello");
	string numbers("0123456789");
	string name("r2d3");
	//string搜索函数返回的是string::size_type值,是一个无符号类型的,用一个int型的或其他带符号类型来保存不是一个好方法
	//find函数中有两个形参find(c,pos),第二个默认的为0,即从第0个字符开始查找字符c
	auto pos1=str1.find("hello");//查找str1中"hello"第一次出现的位置
	auto pos2=str1.rfind("hello");//查找str1中"hello"最后一次出现的位置
	auto pos3=name.find_first_of(numbers);//在name中查找numbers中任意一个字符第一次出现的位置,即第一个数字的下标
	auto pos4=name.find_first_not_of(numbers);//在name中查找第一个不在numbers中的字符,即第一个不为数字的下标
	auto pos5=name.find_last_of(numbers);//name中最后一个数字的下标
	auto pos6=name.find_last_not_of(numbers);//name中最后一个字符的下标
	cout<<"输出 find:"<<endl;
	cout<<"The first place:"<<pos1<<endl;
	cout<<"The last place:"<<pos2<<endl;
	cout<<"The location of first number:"<<pos3<<endl;
	cout<<"The location of first char:"<<pos4<<endl;
	cout<<"The location of last number:"<<pos5<<endl;
	cout<<"The location of last char:"<<pos6<<endl;
	//如果搜索失败,则返回一个无符号数-1
	auto num=str1.find("daxue");
	if(num==-1)
		cout<<"Fail"<<endl;
	cout<<string::npos<<endl;

	//利用find函数查找一个字符串中出现了多少个数字字符
	string::size_type pos=0;
	while((pos=name.find_first_of(numbers,pos))!=string::npos)
	{
		cout<<"found number at index:"<<pos
			<<"element is "<<name[pos]<<endl;
		++pos;
	}

	//逆向搜索
	string river("Mississippi");
	auto first_pos=river.find("is");//从左向右搜索,第一个is出现的位置
	auto last_pos=river.rfind("is");//从右向左搜索,最后一个is出现的位置
	cout<<endl;
	cout<<"first_pos"<<first_pos<<endl;
	cout<<"last_pos"<<last_pos<<endl;


	//compare函数
	int result=0;
	result=numbers.compare(name);
	cout<<endl;
	cout<<result<<endl;


	//整数转换为字符串
	long long int i=425644;
	string str2=to_string(i);//函数实参只能是long long int,unsigned long long int,long double
	cout<<endl<<"输出 long long int to string:"<<str2<<endl;
	//字符串转换为浮点数
	double d=stod(str2);
	cout<<endl<<"输出 string to double/float:";
	cout<<d<<endl;
	string str5="pi=3.14";
	cout<<stof(str5.substr(str5.find_first_of("+-.0123456789")))<<endl;



	//append函数,作用是在string的末尾进行插入操作的一种方法,s.append(args)返回一个指向s的引用
	cout<<endl<<"输出 insert/append/replace:"<<endl;
	string str3("c++ primer");
	string str4=str3;
	str3.insert(str3.size()," 4th Ed.");
	cout<<str3<<endl;
	str4.append(" 4th Ed.");
	cout<<str4<<endl;
	//replace函数,s.replace(range,args)删除s中范围range内的字符,替换为args指定的字符,range或者是一个下标和长度,或者是一对迭代器,返回一个指向s的引用
	str4.replace(11,3,"11th");
	cout<<str4<<endl;

	//insert函数和erase函数
	cout<<endl<<"输出 insert/erase:";
	string str6="ZJU";
	str6.insert(str6.size(),5,'!');//s.insert(pos,args),在pos之前插入args制定的字符
	cout<<str6<<endl;
	string str7=str6.erase(str6.size()-5,5);//s.erase(pos,len)删除从pos开始的len个字符,如果len被省略,则删除从pos开始直至s末尾的所有字符,返回一个指向s的一个引用
	cout<<str7<<" "<<str6<<endl;//s.erase(pos,len)返回一个指向s的引用
	string::iterator iter;
	iter=str6.insert(str6.begin(),'h');//返回一个指向新插入的字符的迭代器
	cout<<(*iter)<<endl;

	//assign函数,s.assign(args),将s中的字符替换为args指定的字符,返回一个指向s的引用
	cout<<endl<<"输出 assign:";
	const char *cp="stately,plump back";
	string str8;
	str8.assign(cp,7);
	cout<<str8<<endl;
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值