C++学习——第6章数组和字符串(下)

本文详细介绍了C++中字符串的各种操作方法,包括搜索、修改、插入、替换和删除等核心功能,并展示了如何利用这些功能来处理复杂的字符串问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


1.       搜索字符串

继续昨天的搜索字符串的学习,还可以搜索非空字符串、C样式字符串中包含的对应于指定字符个数的子字符串,比如将昨日搜索字符串的代码改动一下:

cout << sentence.find(“akat”, 1, 2) <<endl;   //输出为9

其中find()函数的第一个参数是非空字符串,第二个参数是开始搜索的索引位置,第三个参数是非空字符串中药提取作为要查找的字符串的字符个数。

即,在字符串”Manners maketh man”中从索引位置为1的位置开始搜索字符串”akat”中的”ak”字符串,输出得到搜索到的位置为9

 

搜索had字符串,并计算出现字数。

其中这个搜索方式和以上的搜索不同,搜索从index=0的位置开始,在index=19的位置上找到第一个word,并把找到的word的索引位置存储在index中,再执行for循环中的第三个控制表达式(两个操作,一个是索引位置递增index += substr.length()——跳过搜索到字符串的长度,另一个是word出现次数+1),然后循环继续,在index+= substr.length()索引位置上继续搜索,如果存储在index中的值是string::npos,就表示没有找到word,循环就停止。

#include <iostream>
#include <string>

using namespace std;

int main()
{
	//转义字符。。。
	string text = "Smith, where Jones had had \"had had\", had had \"had\".""\n \"Had had\" had had the examiners' approval.";
	string word = "had";

	cout << endl << "The string is: " << endl << text << endl;

	//比较复杂的for循环语句,语句中既进行了非法值的判断index = text.find(word,index)) != string::npos,又对应有字符串个数进行了计数index += word.length(),count++ 
	int count = 0;
	for(int index = 0; (index = text.find(word,index)) != string::npos; index += word.length(),count++);

	cout << "Your text contained "
		 << count << " occurrences of \""
		 << word << "\"."
		 << endl;
	return 0;
}

find_first_of()函数与find_last_of()函数

find_first_of()函数作用从string对象的开头开始搜索,查找给定字符集合中第一次出现的位置,可以用于搜索字符集和中的字符,以下代码输出为5

#include <iostream>
#include <string>

using namespace std;

int main()
{
	string text = "Smith, where Jones had had \"had had\", had had \"had\".""\n \"Had had\" had had the examiners' approval.";
	string separators = ",.\"";

	cout << text.find_first_of(separators)
		 << endl;

	return 0;
}

也可以把参数定义为非空字符串,以下代码输出为2因为第一个元音是i,其索引位置为2。可以用于检测元音字母。

cout << text.find_first_not_of(“AaEeIiOoUu”);       //输出为 2

    << endl;

 

还可以使用find_last_of()函数从string对象的结尾开始,进行逆向搜索,以查找集合中的字符最后一次出现的位置

cout << text.find_first_of(“AaEeIiOoUu”);       //输出为 92

    << endl;

 

另一个选项是查找不在字符集和中的字符,这可以使用find_first_not_of()函数和find_last_not_of函数。以下代码,因为第一个字符不是元音,所以结果就是该字符,其索引值是0。

cout << text.find_first_not_of(“AaEeIiOoUu”);       //输出为 0

    << endl;



逆向搜索字符串

rfind()函数,以下搜索语句都查找rfind()函数中的参数最后一次出现的位置,返回它找到的第一个字符的位置。即,找到参数所在字符串最后一个位置并返回。

	string sentence = "Manners maketh man";
	string word = "an";
	cout << sentence.rfind(word)	    << endl;	//输出16
	cout << sentence.rfind("man")	<< endl;	//输出15
	cout << sentence.rfind('e')		<< endl;	//输出11

同样也可以添加参数,指定从后向前搜索的起始位置;当第一个参数是C样式的字符串时,还可以添加第三个参数,指定从C样式字符串中提取的字符串个数,作为要搜索的子字符串


2.       修改字符串

 

插入字符串insert()函数

以下代码words字符串插入到phrase中索引位置为14的字符前面,输出就为“We can insert a string into a string.”

	string phrase = "We can insert a string.";
	string word = "a string into ";
	phrase.insert(14,words);

当然也可以吧非空字符串插入到string对象中

phrase.insert(14,”a string into ”);

 

更复杂一点的是,把一个string对象的子字符串插入到string类型的对象中。在insert()函数调用中提供另外两个参数。一个参数指定子字符串中第一个字符的索引位置,另一个参数指定子字符串的字符个数。

phrase.insert(13, words, 8, 5);

这行代码的insert函数后两个参数就是把words中从第8个位置开始的5个字符组成的子字符串插入到phrase中。

 

把非空字符串中指定书目的字符插入到string对象中也有类似的效果。

phrase.insert(13, “into something”, 5);

这行代码把“into something”中的前5个字符组成的子字符串插入到phrase中的第13索引位置的字符之前。

 

如果需要把包含几个想吐字符的字符串插入到string对象中,可以用:

phrase.insert(16, 7, ‘*’);

这个语句将7个星号插入到phrase的第16索引位置的字符之前。即,“We can insert a *******string.”


替换子字符串

以下代码将名字Jones替换为一个不常见的名字(两个字符串有不同长度也可以替换),其中replace()函数的几个参数,第一个为索引位置,第二个为选定替换的子字符串长度,第三个为替换字符串。

         string text = "Smith, where Jones had had \"had had\", had had \"had\".""\n \"Had had\" had had the examiners' approval.";

         text.replace(13, 5, "Grutfuttock");

 

替换字符串可以是string对象或非空字符串,以下代码查找text中”Jones”的第一个字母的位置,并把索引值存储在start中,用find_first_of()函数搜索separators中的分隔符,查找”Jones”最后一个字符后面的字符。然后将索引值存储在end,然后两者相减得到字符串的长度。

		string text = "Smith, where Jones had had \"had had\", had had \"had\".""\n \"Had had\" had had the examiners' approval.";
	string separators=" ,.\" ";
	size_t start = text.find("Jones");
	size_t end = text.find_first_of(separators,start+1);
	text.replace(start, end-start,” Gruntfuttock”);

同样替换字符串用string 对象表示:

#include <iostream>
#include <string>

using namespace std;

int main()
{
	string text = "Smith, where Jones had had \"had had\", had had \"had\".""\n \"Had had\" had had the examiners' approval.";
	string separators=" ,.\" ";
	size_t start = text.find("Jones");					//找到开头
	size_t end = text.find_first_of(separators,start+1);	//找到结尾
	text.replace(start, end-start, name, 5, 12);			//开始的位置,结束的位置,插入的字符串name,name的第5个位置的字符开始,一共12个字符。
	return 0;
}

 

删除字符串中的字符

erase()函数

删除从索引值为0开始的6个字符

text.erase(0, 6);

 

通常使用这个函数删除以前搜索出来的某个字符串:

	string word = "rose";
	size_t index = text.find(word);
	if (index != string::npos)
		text.erase(index,word.length());

chear()函数:删除字符串中的所有字符

text.clear();

 

3.       string类型的数组

使用string类型的对象数组与使用其他类型的数组是一样的。

string words[] = {“this”, “that”, “theother”};

数组words有3个元素,当然也可以显示指定数组的大小

string words[10] = {“this”, “that”, “theother”};

现在,该string类型数组有10个元素,前3个元素用花括号中的字符串字面量初始化,其他的7个是空字符串。

还可以引用string数组元素中的各个字符。

下面的语句中将第3个元素的第7个字符改为t:

words[2][6] = ‘t’;

然后输出:

cout << words[2];

显示为:

the otter

 

4.       宽字符的字符串(不理解)

声明用wstring ,字符串的字面量在双引号中包含wchat_t类型的字符串,添加一个前缀L,把它们与包含char字符的字符串字面量区分开。

输出用wcout流

在wstring对象的操作中,应使用wchat_t类型的字符变量,定义字符和字符串字面量时要加上前缀L。

#include <iostream>
#include <string>

using namespace std;

int main()
{
	wstring word = L"rose";
	wcout << word;

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值