C ++字符串支持常见的比较操作符(>,>=,<,<=,==,!=),甚至支持string与C-string的比较(如 str<”hello”)。在使用>,>=,<,<=这些操作符的时候是根据”当前字符特性”将字符按字典顺序进行逐一的比较。字典排序靠前的字符小,比较的顺序是从前向后比较,遇到不相等的字符就按这个位置上的两个字符的比较结果确定两个字符串的大小。
if (str1.compare(str2) != 0) std::cout << str1 << " is not " << str2 << '\n';//green apple is not red apple //str1的第6个字符以及后面的4个字符和参数比较 if (str1.compare(6,5,"apple") == 0) std::cout << "still, " << str1 << " is an apple\n";//still, green apple is an apple
if (str2.compare(str2.size()-5,5,"apple") == 0) std::cout << "and " << str2 << " is also an apple\n";//and red apple is also an apple //str1的第6个字符以及后面的4个字符和str2的第4个字符以及后面的4个字符比较 if (str1.compare(6,5,str2,4,5) == 0) std::cout << "therefore, both are apples\n";//therefore, both are apples system("pause"); return0; }
intmain() { string base="this is a test string."; string str2="n example"; string str3="sample phrase"; string str4="useful.";
// replace signatures used in the same order as described above:
// Using positions: 0123456789*123456789*12345 string str=base; // "this is a test string." //第9个字符以及后面的4个字符被str2代替 str.replace(9,5,str2); // "this is an example string." (1) //第19个字符串以及后面的5个字符用str的第7个字符以及后面的5个字符代替 str.replace(19,6,str3,7,6); // "this is an example phrase." (2) //第8个字符以及后面的9个字符用字符串参数代替 str.replace(8,10,"just a"); // "this is just a phrase." (3) //第8个字符以及后面的5个字符用字符串参数的前7个字符替换 str.replace(8,6,"a shorty",7); // "this is a short phrase." (4) //第22以及后面的0个字符用3个叹号替换 str.replace(22,1,3,'!'); // "this is a short phrase!!!" (5) //迭代器的原理同上 // Using iterators: 0123456789*123456789* str.replace(str.begin(),str.end()-3,str3); // "sample phrase!!!" (1) str.replace(str.begin(),str.begin()+6,"replace"); // "replace phrase!!!" (3) str.replace(str.begin()+8,str.begin()+14,"is coolness",7); // "replace is cool!!!" (4) str.replace(str.begin()+12,str.end()-4,4,'o'); // "replace is cooool!!!" (5) str.replace(str.begin()+11,str.end(),str4.begin(),str4.end());// "replace is useful." (6) cout << str << '\n'; system("pause"); return0; }
intmain() { stringstr("There are two needles in this haystack with needles."); stringstr2("needle");
//在str当中查找第一个出现的needle,找到则返回出现的位置,否则返回结尾 size_t found = str.find(str2);//size_t = unsigned long long if (found != string::npos) cout << "first 'needle' found at: " << found << '\n';//first 'needle' found at: 14(n的下角标) //在str当中,从第found+1的位置开始查找参数字符串的前6个字符 found = str.find("needles are small",found+1,6); if (found != string::npos) cout << "second 'needle' found at: " << found << '\n';//second 'needle' found at: 44 //在str当中查找参数中的字符串 found = str.find("haystack"); if (found != string::npos) cout << "'haystack' also found at: " << found << '\n';//'haystack' also found at: 30 //查找一个字符 found = str.find('.'); if (found != string::npos) cout << "Period found at: " << found << '\n';//Period found at: 51 //组合使用,把str2用参数表中的字符串代替 // let's replace the first needle: str.replace(str.find(str2),str2.length(),"preposition"); cout << str << '\n';//There are two prepositions in this haystack with needles. system("pause"); return0; }
intmain() { std::stringstr1("Please, replace the vowels in this sentence by asterisks."); std::size_t found1 = str1.find_first_of("aeiou"); //把所有元音找出来用*代替 while (found1 != std::string::npos) { str1[found1] = '*'; found1 = str1.find_first_of("aeiou",found1+1); } std::cout << str1 << '\n';//Pl**s*, r*pl*c* th* v*w*ls *n th*s s*nt*nc* by *st*r*sks.
//在str2中找到第一个不是消协英文字母和空格的字符 std::stringstr2("look for non-alphabetic characters..."); std::size_t found2 = str2.find_first_not_of("abcdefghijklmnopqrstuvwxyz "); if (found2 != std::string::npos) { std::cout << "The first non-alphabetic character is " << str2[found2]; std::cout << " at position " << found2 << '\n'; //The first non-alphabetic character is - at position 12 } system("pause"); return0; }