您的位置:首页 > 编程语言 > C语言/C++

实战c++中的string系列--string的分割、替换(类似string.split或是explode())

2015-12-10 23:58 471 查看
对一个字符串根据某个字符进行分割也是在实战中经常遇到的问题,也是面试中经常会被人提及的。

如果你是个C Sharp程序员,你会知晓string.split函数,有下面这些重载:

1) public string[] Split(params char[] separator)

2) public string[] Split(char[] separator, int count)

3) public string[] Split(char[] separator, StringSplitOptions options)

4) public string[] Split(string[] separator, StringSplitOptions options)

5) public string[] Split(char[] separator, int count, StringSplitOptions options)

6) public string[] Split(string[] separator, int count, StringSplitOptions options)

如果你是个PHP程序员,你也会使用explode方法。

但是如果你是C++程序员,或是进行C++开发,那么这里的string就没有现成的分割方法。我们需要自行实现。

const vector<string> explode(const string& s, const char& c)
{
string buff{""};
vector<string> v;

for(auto n:s)
{
if(n != c) buff+=n;
elseif(n == c && buff != "") { v.push_back(buff); buff = ""; }
}
if(buff != "") v.push_back(buff);

return v;
}

//使用自定义的字符串分割函数
int main()
{
string str{"the quick brown fox jumps over the lazy dog"};
vector<string> v{explode(str, ' ')};
for(auto n:v) cout << n << endl;

return 0;
}
//输出如下:
the
quick
brown
fox
...


下面是另一种形式:

int split(const string& str, vector<string>& ret_, string sep = ",")
{
if (str.empty())
{
return 0;
}

string tmp;
string::size_type pos_begin = str.find_first_not_of(sep);
string::size_type comma_pos = 0;

while (pos_begin != string::npos)
{
comma_pos = str.find(sep, pos_begin);
if (comma_pos != string::npos)
{
tmp = str.substr(pos_begin, comma_pos - pos_begin);
pos_begin = comma_pos + sep.length();
}
else
{
tmp = str.substr(pos_begin);
pos_begin = comma_pos;
}

if (!tmp.empty())
{
ret_.push_back(tmp);
tmp.clear();
}
}
return 0;
}


=============================================================

其他语言的string也有replace的方法,那么再c++中我们也可以自己实现这个方法:

string replace(const string& str, const string& src, const string& dest)
{
string ret;

string::size_type pos_begin = 0;
string::size_type pos       = str.find(src);
while (pos != string::npos)
{
cout <<"replacexxx:" << pos_begin <<" " << pos <<"\n";
ret.append(str.data() + pos_begin, pos - pos_begin);
ret += dest;
pos_begin = pos + 1;
pos       = str.find(src, pos_begin);
}
if (pos_begin < str.length())
{
ret.append(str.begin() + pos_begin, str.end());
}
return ret;
}


================================================================

最后介绍一个C中的函数,用于截取字符串:

原型:extern char *strtok(char *s, char *delim);

#include <stdio.h>
#include <string.h>

int main ()
{
char str[] ="- This, a sample string.";
char * pch;
printf ("Splitting string \"%s\" into tokens:\n",str);
pch = strtok (str," ,.-");
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, " ,.-");
}
return 0;
}


输出:

Splitting string “- This, a sample string.” into tokens:

This

a

sample

string
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: