【C++】string的部分接口补充(swap()、replace()、suffix()、find())

本文介绍了C++中string类的几个重要接口,包括swap()用于交换字符串,replace()用于替换字符串内容,suffix()获取子字符串,特别是后缀,以及find()用于查找字符串中特定子串的位置。

开发板推荐:天空星STM32F407VET6开发板

超高性价比 STM32主控 | 超高主频 | 一板兼容百芯 | 比赛神器 | 沉金彩色丝印

swap()

swap()接口表示交换,有以下两种编写方式 

void test_string()
{
	string s1("hello");
	string s2("world");
	cout << s1 << "--" << s2 << endl;
	s1.swap(s2);//单纯地成员变量的交换,指针交换,代价小
	cout << s1 << "--" << s2 << endl;
	swap(s1, s2); //产生了一个临时对象,拷贝构造,再赋值,再赋值,代价大,效率低
	cout << s1 << "--" << s2 << endl;
}

两种方法都可以实现交换,但是代价不一样,更倾向于使用s1.swap(s2) 

 

 

replace()

replace()是string类中经常被使用的一个接口,功能是替换。下面以一个例子来说明。

void test_string1()
{
	string url = "https://blog.csdn.net/Miss_Monster";
	cout << url << endl;
	//从0开始的5个字符替换为"http",但是代价较大。
	//如果不是一等一替换,字符需要一个一个挪动一次位置
	url.replace(0, 5, "http");
	cout <<"替换后为:"<< url << endl;
	cout << endl;

	url[5] = '\0';//将url的第五个字符改为'\0'
	cout << url << endl;//cout遇到'\0'不会停止
	cout <<"C形式的字符串输出为:"<< url.c_str() << endl;
	printf("printf输出%s\n", url.c_str());//C形式的字符串是以'\0'结束
	cout << endl;
}

 

suffix()

suffix()接口可以理解为获取子字符串,常用于获取后缀等...

之前我们也用过suffix接口,也知道这个是专门获取后缀的,但是如果直接使用这个接口编写的代码是硬编码,非常生硬,一旦字符串发生改变,这个接口的参数也要改变,适用性不强,所以我们将它与find()进行配合。

void test_string2()
{
	string file = "test.cpp";
	//硬编码(写死的生硬的编码),从字符串的第4个位置开始取4个字符
	//一旦改变字符串,或者字符串很长,适用性就不普遍了,这样就不可取
	string suffix(file, 4, 4);
	cout << suffix << endl;

	size_t pos = file.find('.');//如果找到了就返回第一次出现的位置
	if (pos != string::npos)//否则返回npos(npos是它的一个静态成员的const变量,npos=-1)
	{
		string suffix(file, pos, file.size() - pos);
		cout << suffix << endl;
	}
}

find()

find()接口顾名思义就是查找,查找一个字符串是否在调用的字符串中出现过。

同类型的还有rfind,找最后一个出现的匹配字符串,返回的位置是从前往后数的

string getfilesuffix(const string& file)
{
	//size_t pos = file.find('.'); //返回第一次出现的位置
	size_t pos = file.rfind('.');  //返回最后一次出现的位置

	string suffix;
	if (pos != string::npos)
	{
		//suffix = file.substr(pos);	//取到最后
		suffix = file.substr(pos, file.size() - pos);//根据算出要取出的位数取
	}
	return suffix;
}
void test_string()
{
	cout << getfilesuffix("test.cpp") << endl;
	cout << getfilesuffix("test.c") << endl;

	cout << getfilesuffix("test.tar.zip") << endl;//若为这种形式,则用rfind找最后一个后缀

	//.zip
	const string file = "test.cpp.tar.zip";
	size_t pos = file.rfind('.'); //最后一次出现的位置
	string suffix;
	if (pos != string::npos)
	{
		suffix = file.substr(pos, file.size() - pos);
	}
	cout << suffix << endl;

	//.tar
	size_t prevpos = file.rfind('.', pos - 1);//从pos-1的位置上倒着找
	if (pos != string::npos)
	{
		suffix = file.substr(prevpos, pos - prevpos);
	}
	cout << suffix << endl;

	//找域名
	string url("https://blog.csdn.net/Miss_Monster");
	cout << url << endl;
	size_t start = url.find("://");//找字符串
	if (start == string::npos)
	{
		cout << "invalid url" << endl;
		return;
	}
	start += 3;
	size_t finish = url.find('/', start);
	string address = url.substr(start, finish - start);
	cout << address << endl;
}

开发板推荐:天空星STM32F407VET6开发板

超高性价比 STM32主控 | 超高主频 | 一板兼容百芯 | 比赛神器 | 沉金彩色丝印

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值