赶紧的,先发一篇,最基本的string C++
来源:互联网 发布:密立根油滴实验数据 编辑:程序博客网 时间:2024/05/16 10:08
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;
- }
- 赶紧的,先发一篇,最基本的string C++
- 还好就赶紧赶紧发的地方
- 先发一个简单的C#的程序结构
- 发一篇长的
- 我的blog搬到CSDN了,先发一篇留做纪念,呵呵
- 很不错的学习VIM的一篇文章,赶紧MARK。
- 先发一篇试一下
- 第一篇,先发一篇看看.
- c++string的基本操作
- c中最基本的
- String是最基本的数据类型吗?
- String 是最基本的数据类型吗?
- String是最基本的数据类型吗?
- String是最基本的数据类型吗
- String是最基本的数据类型吗?
- String是最基本的数据类型吗?
- spring-shiro最基本的配置,就这一篇入门
- spring-shiro最基本的配置,就这一篇入门
- 在 360浏览器下 position:relative; 不随着滚动条的滚动而滚动而是飘在页面上 解决方法 给父级也就是出现滚动条的元素添加 position:relative;就ok l了
- linux命令大全
- 关于gdb的几个基本命令
- C++ Excel操作
- js如何实现打印指定区域的内容
- 赶紧的,先发一篇,最基本的string C++
- ffmpeg+live555+mplayer搭建简单的视频聊天服务器
- 怎么spring security 和maven,spring,结合jar包冲突和依赖关系问题解决办法。
- python中讲正则
- block简单用法 与传统回调 ---写的不错文章
- 操作libcurl停止文件下载类名目开发(一) libcurl简略操作引见
- [COCOS2D-X]裁剪实现
- 老张琢磨的运用CSS3和jQuery制作不错的翻页效果
- 链表的将基本结构及用法