//n, len2, pos2 都是无符号值 string s(cp, n) //s是cp指向的数组中前n个字符的拷贝。此数组至少应该包含n个字符 string s(s2, pos2) //s是string s2从下标pos2开始的字符的拷贝。 string s(s2, pos2, len2) //s是s2从pos2开始Len2个字符
const char *cp="Hello World!!!"; //以空字符结束的数组 char noNull[]={'H', 'i'}; //不是以空字符结束 string s1(cp); //拷贝cp中的字符,直到遇到空字符;s1==“Hello World!!!” string s2(noNull, 2); //从noNull拷贝两个字符;s2=="Hi" string s3(noNull); //未定义 string s4(cp+6, 5); //从cp[6]开始拷贝5个字符 string s5(cp, 6, 5); //从cp第6个字符开始,拷贝5个字符 string s6(cp, 6); //从第6个拷贝到最后 string s7(s1, 6, 20); //正确,只拷贝到s1末尾;s7=="World!!!" string s8(s1, 16); //抛出异常out_of_range
string s("hello world"); string s2=s.substr(0,5); //s2=hello string s3=s.substr(6); //s3=world string s4=s.substr(6, 11); //s4=world string s5=s.substr(12); //抛出一个out_of_range异常
string s("C++ Primer"), s2=s; //将s和s2初始化为C++ Primer s.insert(s.size(), "4th Ed."); //s==“C++ Primer 4th Ed。” s2.append("4th Ed."); //和上面的等价 s.erase(11, 3); //s=="C++ Primer Ed." s.insert(11, "5th"); //s=="C++ Primer 5th Ed." //从位置11开始,删除3个字符并插入"5th" s2.replace(11, 3, "Fifth"); //s=="C++ Primer Fifth Ed."
<span style="font-weight: normal;">#include<iostream> #include<string> using namespace std; int main() { string s="hehe",oldVal="haha",newVal="myLove"; s.insert(2, oldVal); s.insert(2, oldVal); s.insert(2, oldVal); s.erase(2, 12); s.insert(0, newVal); s.erase(6, 4); cout<<s; return 0; }</span>
<span style="font-weight: normal;">string name("AnnaBelle"); auto pos1=name.find("Anna"); //pos1==0</span>
string numbers("0123456789"), name("r2d2"); //返回1,即,name中第一个数字的下标 auto pos=name.find_first_of(numbers);
string dept("03714p3"); //返回5--字符'p'下标 auto pos=dept.find_first_not_of(numbers);
string::size_type pos=0; //每步循环查找name中下一个数 while((pos=name.find_first_of(numbers, pos)) != string::npos) { cout<<"found number at index: "<<pos <<" element is "<<name[pos]<<endl; ++pos; }
#include<iostream> #include<string> using namespace std; int main() { string s1="ab2c3d7R4E6",s2="abcdRE",s3="23467"; string::size_type pos=0; cout<<"使用find_first_of查找!!!"<<endl; //在s2中第pos个位置开始找,找到第一个存在于s1中的元素位置 while((pos=s1.find_first_of(s3, pos)) != string::npos) { cout<<"found number at index: "<<pos <<"数字 element is "<<s1[pos]<<endl; ++pos; } cout<<">--------------------------------------<"<<endl; pos=0; while((pos=s1.find_first_of(s2, pos)) != string::npos) { cout<<"found number at index: "<<pos <<"字符 element is "<<s1[pos]<<endl; ++pos; } cout<<"使用find_first_not_of查找!!!"<<endl; pos=0; while((pos=s1.find_first_not_of(s2, pos)) != string::npos) { cout<<"found number at index: "<<pos <<"数字 element is "<<s1[pos]<<endl; ++pos; } cout<<">--------------------------------------<"<<endl; pos=0; while((pos=s1.find_first_not_of(s3, pos)) != string::npos) { cout<<"found number at index: "<<pos <<"字符 element is "<<s1[pos]<<endl; ++pos; } return 0; }
/** * 功能:额外的string操作 * 时间:2014年6月13日08:07:58 * 作者:cutter_point */ #include<iostream> #include<string> using namespace std; int main() { string s1="ab2c3d7R4E6",s2="abcdRE",s3="23467"; string::size_type pos=0; cout<<"使用find_first_of查找!!!"<<endl; //在s2中第pos个位置开始找,找到第一个存在于s1中的元素位置 while((pos=s1.find_first_of(s3, pos)) != string::npos) { cout<<"found number at index: "<<pos <<"数字 element is "<<s1[pos]<<endl; ++pos; } cout<<">--------------------------------------<"<<endl; pos=0; while((pos=s1.find_first_of(s2, pos)) != string::npos) { cout<<"found number at index: "<<pos <<"字符 element is "<<s1[pos]<<endl; ++pos; } cout<<"使用find_first_not_of查找!!!"<<endl; pos=0; while((pos=s1.find_first_not_of(s2, pos)) != string::npos) { cout<<"found number at index: "<<pos <<"数字 element is "<<s1[pos]<<endl; ++pos; } cout<<">--------------------------------------<"<<endl; pos=0; while((pos=s1.find_first_not_of(s3, pos)) != string::npos) { cout<<"found number at index: "<<pos <<"字符 element is "<<s1[pos]<<endl; ++pos; } return 0; } /* //n, len2, pos2 都是无符号值 string s(cp, n) //s是cp指向的数组中前n个字符的拷贝。此数组至少应该包含n个字符 string s(s2, pos2) //s是string s2从下标pos2开始的字符的拷贝。 string s(s2, pos2, len2) //s是s2从pos2开始Len2个字符 const char *cp="Hello World!!!"; //以空字符结束的数组 char noNull[]={'H', 'i'}; //不是以空字符结束 string s1(cp); //拷贝cp中的字符,直到遇到空字符;s1==“Hello World!!!” string s2(noNull, 2); //从noNull拷贝两个字符;s2=="Hi" string s3(noNull); //未定义 string s4(cp+6, 5); //从cp[6]开始拷贝5个字符 string s5(cp, 6, 5); //从cp第6个字符开始,拷贝5个字符 string s6(cp, 6); //从第6个拷贝到最后 string s7(s1, 6, 20); //正确,只拷贝到s1末尾;s7=="World!!!" string s8(s1, 16); //抛出异常out_of_range string s("hello world"); string s2=s.substr(0,5); //s2=hello string s3=s.substr(6); //s3=world string s4=s.substr(6, 11); //s4=world string s5=s.substr(12); //抛出一个out_of_range异常 string s("C++ Primer"), s2=s; //将s和s2初始化为C++ Primer s.insert(s.size(), "4th Ed."); //s==“C++ Primer 4th Ed。” s2.append("4th Ed."); //和上面的等价 s.erase(11, 3); //s=="C++ Primer Ed." s.insert(11, "5th"); //s=="C++ Primer 5th Ed." //从位置11开始,删除3个字符并插入"5th" s2.replace(11, 3, "Fifth"); //s=="C++ Primer Fifth Ed." */ /* #include<iostream> #include<string> using namespace std; int main() { string s="hehe",oldVal="haha",newVal="myLove"; s.insert(2, oldVal); s.insert(2, oldVal); s.insert(2, oldVal); s.erase(2, 12); s.insert(0, newVal); s.erase(6, 4); cout<<s; return 0; } string name("AnnaBelle"); auto pos1=name.find("Anna"); //pos1==0 string numbers("0123456789"), name("r2d2"); //返回1,即,name中第一个数字的下标 auto pos=name.find_first_of(numbers); string dept("03714p3"); //返回5--字符'p'下标 auto pos=dept.find_first_not_of(numbers); string::size_type pos=0; //每步循环查找name中下一个数 while((pos=name.find_first_of(numbers, pos)) != string::npos) { cout<<"found number at index: "<<pos <<" element is "<<name[pos]<<endl; ++pos; } */
【足迹C++primer】28、额外的string操作,布布扣,bubuko.com
原文地址:http://blog.csdn.net/cutter_point/article/details/30452491