字符串
来源:互联网 发布:wtf什么意思网络用语 编辑:程序博客网 时间:2024/06/05 19:26
1、初始化
string proverb("Many a mickle make a muckle.");string phrase(proverb,0,13); //Many a mickle 第一个参数是作为初始化源的string对象名称,第二个参数是string对象起始索引,第三个参数是用作初始值的字符个数
string phrase="The higher the fewer";string word=phrase.substr(4,6);//higher 第一个参数指定子字符串开始的索引位置,第二个参数指定子字符串中的字符个数
2、compare()
word.compare("and")如果word大于and返回正整数,等于返回0,小于返回负整数
string word1="A jackhammer";string word2="jack";if(word1.compare(2,4,word2)==0) //compare第一个参数是word1中子字符串的起始索引位置,第二个参数是子字符串的字符个数,即jackstring word1="A jackhammer";string word2="It is a jack_in_the_box";if(word1.compare(24,word2,8,4)==0)//后两个参数是word2的子字符串的索引位置和长度if(word1.compare(2,4,"jacket",4)==0)//最后一个参数是jacket的前四个字符,即jack cout <<"Equal"<<endl; cout <<"Equal"<<endl; cout <<"Equal" <<endl;
3、搜索字符串
find()返回搜索到的第一个子字符串中第一个字符的索引位置
string sentence="Manners maketh man";string word="man";cout <<sentence.find(word)<<endl;//15cout <<sentence.find('x')<<endl;//string::nposcout <<sentence.find("an",3)<<endl;//16 从指定索引位置搜索cout<<sentence.find("akat",1,2)<<endl;//9 第二个参数是开始搜索的索引位置,第三个参数是第一个参数中提取作为要查找的字符串的字符个数
string sentence="Manners maketh man";string word="an";int count=0;size_t position=0;for(size-t i=0;i<=sentence.length()-word.length();){ position=sentence.find(word,i); if(positon==string::npos) break; count++; i=position+1; //允许重叠找到} for(int index=0;(index=text.find(word,index))!=string::npos;index+=word.length(),count++)//不重叠;
在字符串中搜索字符集合中的字符
// Program 6.10 Searching a string for characters from a set#include <iostream>#include <string>using std::cout;using std::endl;using std::string;int main() { // The string to be searched string text = "Smith, where Jones had had \"had had\", had had \"had\"." " \"Had had\" had had the examiners' approval."; string separators = " ,.\"\'"; // Word delimiters // Find the start of the first word size_t start = text.find_first_not_of(separators); size_t end = 0; // Index for the end of a word // Now find and output the words int word_count = 0; // Number of words while(start != string::npos) { end = text.find_first_of(separators, start + 1); // Find end of word第二个参数是要进行搜索的字符串中,从哪里开始搜索 if(end == string::npos) // Found a separator? end = text.length(); // No, so set to last + 1 cout << text.substr(start, end - start) // Output the word << endl; word_count++; // Increase the count // Find the first character of the next word start = text.find_first_not_of(separators, end + 1); } cout << "Your string contained " << word_count << " words." << endl; return 0;}
逆向搜索rfind()和find_last_of(),find_last_not_of用法和以上类似
4、插入、替换、删除字符串
string phrase="We can insert a string.";string words="a string into ";phrase.insert(14,words);phrase.insert(13,words,8,5);phrase.insert(13,"into something",5);phrase.insert(16,7,'*');
find()返回搜索到的第一个子字符串中第一个字符的索引位置
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- Python中的metaclass
- mysql amoeba读写分离
- java编程思想笔记--多态部分四
- jQuery性能优化注意事项
- [zt]所谓“抗日老兵”“抗战名将”有多少掺水造假?
- 字符串
- sql server中filegroup与partition解析
- php三中页面跳转方式(header、location、refresh) 乐杨俊
- ASIHTTPRequest 详解, http 请求终结者
- 感悟
- java jquery扩展(prototype)
- IOS开发网络篇之──ASIHTTPRequest下载示例(支持断点续传)
- android中Webview与javascript的交互(互相调用)
- matlab中knnsearch平滑处理