代码整理
2019-03-27 本文已影响0人
去年匆匆今年匆匆
split_string
vector<string> split(const string &s, const string &seperator){
vector<string> result;
typedef string::size_type string_size;
string_size i = 0;
while(i != s.size()){
//找到字符串中首个不等于分隔符的字母;
int flag = 0;
while(i != s.size() && flag == 0){
flag = 1;
for(string_size x = 0; x < seperator.size(); ++x)
if(s[i] == seperator[x]){
++i;
flag = 0;
break;
}
}
//找到又一个分隔符,将两个分隔符之间的字符串取出;
flag = 0;
string_size j = i;
while(j != s.size() && flag == 0){
for(string_size x = 0; x < seperator.size(); ++x)
if(s[j] == seperator[x]){
flag = 1;
break;
}
if(flag == 0)
++j;
}
if(i != j){
result.push_back(s.substr(i, j-i));
i = j;
}
}
return result;
}
void split_string(const std::string& s, std::vector<std::string>& v, const std::string& c)
{
std::string::size_type pos1, pos2;
pos2 = s.find(c);
pos1 = 0;
while (std::string::npos != pos2)
{
v.push_back(s.substr(pos1, pos2 - pos1));
pos1 = pos2 + c.size();
pos2 = s.find(c, pos1);
}
if (pos1 != s.length())
v.push_back(s.substr(pos1));
}
C++string函数
赋值assign()
将字符串2赋值给字符串1:
- 字符串1 = 字符串2;
- 字符串1.assign(字符串2);
string s1="I'm";
string s2;
s2="1234";
s1.assign(s2);
cout<<s1;
//输出结果:1234
将字符串2从第m个字符开始的n个字符赋值给字符串1:
字符串1.assign(字符串2, m, n);
string s1="I'm";
string s2="12345678";
s1.assign(s2,4,3);
cout<<s1;
//输出结果:567
长度length()、size()
返回字符串长度: 1. 字符串1.length(); 2. 字符串1.size();
string s1 = "1234Juruo1234";
cout << s1.length() << endl;
cout << s1.size() <<endl;
//输出结果:13
比较
">", "<", "==", ">=", "<="均可以用于字符串比较。
string PPAP[] = {"I", "have", "a", "pen", "an", "apple", "um", "apple-pen"};
sort(PPAP, PPAP + 8);
for(int i = 0; i < 8; i++){
cout << PPAP[i] << endl;
}
/*
输出结果:
I
a
an
apple
apple-pen
have
pen
um
*/
查找
在字符串1中从第m个字符开始查找字符串2,返回第一次出现的首字母位置,失败时返回-1:
字符串1.find(字符串2, m);
string s1 = "ggabcdabcgggabcdefg";
string s2 = "gg";
int pos = -1;
while(1){
pos = s1.find(s2, pos+1);
if(pos == -1) break;
cout << pos << ' ';
}
//输出结果:0 9 10
在字符串1中从第m个字符开始从后向前查找字符串2,返回第一次出现的首字母位置,失败时返回-1:
字符串1.rfind(字符串2, m);
string s1 = "ggabcdabcgggabcdefg";
string s2 = "gg";
int pos = s1.length();
while(pos > 0){
pos = s1.rfind(s2, pos-1);
if(pos < 0) break;
cout << pos << ' ';
}
//输出结果:10 9 0;
连接
将字符串2接到字符串1尾部:
- 字符串1.append(字符串2); //字符不可
- 字符串1 += 字符串2; //字符亦可
string s1 = "I'm ";
string s2 = "Juruo";
s1.append(s2);// 或 s1 += s2;
cout << s1;//输出结果:I'm Juruo
将字符串2从第m个字符开始的n个字符接到字符串1尾部:
字符串1.append(字符串2, m, n);
string s1 = "I'm ";
string s2 = "1234Juruo1234";
s1.append(s2, 4, 5);
cout << s1;//输出结果:I'm Juruo
交换
字符串1.swap(字符串2);
string s1 = "I'm ";
string s2 = "Juruo";
s1.swap(s2);
cout << s1 << endl;//输出结果:Juruo
子串
返回字符串1从第m个字符开始的n个字符所组成的子串:
字符串1.substr(m, n);
string s1 = "I'm ";
string s2 = "1234Juruo1234";
s1 = s2.substr(4, 5);
cout << s1 << endl;//输出结果:Juruo
替换
在字符串1中删除从m开始的n个字符,然后在m处插入串s2
字符串1.replace(m, n, s2);
string s1 = "I'm Juruo";
string s2 = "Juruo";
string s3 = "Dalao";
int pos = s1.find(s2);
s1.replace(pos, s2.length(), s3);
cout << s1;//输出结果:I'm Dalao
插入
在字符串1的第m个字符处插入字符串2:
字符串1.insert(m, 字符串2);
string s1 = "I'm Juruo";
string s2 = "not ";
s1.insert(s1.find("Juruo", 0), s2 );
cout << s1 << endl;
//输出结果:I'm not Juruo
删除
从字符串1的第m个字符开始,删除n个字符:
字符串1.erase(m, n);
string s1 = "I'm not Dalao";
s1.erase(s1.find("not"), 4);
cout << s1 << endl;