string已经在多次编程题目中拯救了我,所以今天就来总结一下他吧。
各种各样的匹配函数
find();正向匹配,返回值为第一个匹配的位置,匹配不到返回npos
// string::find
#include <iostream>
#include <string>
int main ()
{
std::string str ("There are two needles in this haystack with needles.");
std::string str2 ("needle");
// different member versions of find in the same order as above:
std::string::size_type found = str.find(str2);
if (found!=std::string::npos)
std::cout << "first 'needle' found at: " << found << '\n';
found=str.find("needles are small",found+1,6);//从found+1开始,匹配前六个字符 "needle"
if (found!=std::string::npos)
std::cout << "second 'needle' found at: " << found << '\n';
found=str.find("haystack");
if (found!=std::string::npos)
std::cout << "'haystack' also found at: " << found << '\n';
found=str.find('.');
if (found!=std::string::npos)
std::cout << "Period found at: " << found << '\n';
// let's replace the first needle:
str.replace(str.find(str2),str2.length(),"preposition"); //找到str2并将其替换成"preposition"
std::cout << str << '\n';
return 0;
/*
first 'needle' found at: 14
second 'needle' found at: 44
'haystack' also found at: 30
Period found at: 51
There are two prepositions in this haystack with needles
*/
}
rfind();逆向匹配
find_first_of()//给定字符集,查找这个字符集任何一个字符缩在字符串中第一个位置。返回值为下标。
// string::find_first_of
#include <iostream> // std::cout
#include <string> // std::string
#include <cstddef> // std::size_t
int main ()
{
std::string str ("Please, replace the vowels in this sentence by asterisks.");
std::size_t found = str.find_first_of("aeiou");
while (found!=std::string::npos)
{
str[found]='*';
found=str.find_first_of("aeiou",found+1);
}
std::cout << str << '\n';
return 0;
}
find_first_not_of();
ind_last_of();
find_last_not_of();//名字取得好,与上面对比
basic_string substr (size_type pos = 0, size_type len = npos) const;//返回一个子串。
pos为子串的起始下标。len为子串的长度,默认为取到结尾。
// string::substr
#include <iostream>
#include <string>
int main ()
{
std::string str="We think in generalities, but we live in details.";
// (quoting Alfred N. Whitehead)
std::string str2 = str.substr (12,12); // "generalities"
std::string::size_type pos = str.find("live"); // position of "live" in str
std::string str3 = str.substr (pos); // get from "live" to the end
std::cout << str2 << ' ' << str3 << '\n';
return 0;
}
#include <string>
#include <iostream>
using namespace std;
int main()
{
string strinfo=" //*---Hello Word!......------";
string strset="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
int first = strinfo.find_first_of(strset);
if(first == string::npos)
{
cout<<"not find any characters"<<endl;
return -1;
}
int last = strinfo.find_last_of(strset);
if(last == string::npos)
{
cout<<"not find any characters"<<endl;
return -1;
}
cout << strinfo.substr(first, last - first + 1)<<endl;//指定下标范围string substr (size_t pos = 0, size_t len = npos) const;
return 0;
}
replace()替代函数
// replacing in a string
#include <iostream>
#include <string>
int main ()
{
std::string base="this is a test string.";
std::string str2="n example";
std::string str3="sample phrase";
std::string str4="useful.";
// replace signatures used in the same order as described above:
// Using positions: 0123456789*123456789*12345
std::string str=base; // "this is a test string."
str.replace(9,5,str2); // "this is an example string." (1)
str.replace(19,6,str3,7,6); // "this is an example phrase." (2)
str.replace(8,10,"just a"); // "this is just a phrase." (3)
str.replace(8,6,"a shorty",7); // "this is a short phrase." (4)
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)
std::cout << str << '\n';
return 0;
}
(1)从第9位开始后面5位替换为str2
(2)str的19位后面6位用str3的第七位后面6位代替
(4)str的第8位后面6位用字符串的前7位代替
(5)str的第23位用3个!代替
各种各样的转换函数
double stod (const string& str, size_t* idx = 0);
*idx是一个传出参数,被设置为被转换了的字符下标+1。
再配合substr(idx),可完成2个数字的切分。
std::string orbits ("365.24 29.53");
std::string::size_type sz; // alias of size_t 是一个unsign int类型
double earth = std::stod (orbits,&sz);
double moon = std::stod (orbits.substr(sz));
std::cout << "The moon completes " << (earth/moon) << " orbits per Earth year.\n";
类似的还有
stof();
stoi();
stol();
stold();
stoll();
stoul();
stoull();
to_string();把别的类型转换为string类型。
杂项
pop_back();
push_back();//与vector的pop_back()和push_back()差不多。
// string::pop_back
#include <iostream>
#include <string>
int main ()
{
std::string str ("hello world!");
str.pop_back();
std::cout << str << '\n';//hello world
return 0;
}