实战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就没有现成的分割方法。我们需要自行实现。
下面是另一种形式:
=============================================================
其他语言的string也有replace的方法,那么再c++中我们也可以自己实现这个方法:
================================================================
最后介绍一个C中的函数,用于截取字符串:
原型:extern char *strtok(char *s, char *delim);
输出:
Splitting string “- This, a sample string.” into tokens:
This
a
sample
string
如果你是个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
相关文章推荐
- leetcode笔记:Word Ladder
- LeetCode题解:Range Sum Query - Immutable(C++版)
- 折半查找算法的实现
- C语言之NULL说明
- C语言数据类型
- Effective C++ 读书笔记3
- [c++]杨辉三角
- 关于实现qsort函数,回调函数
- C++primer之格式控制.
- 关于理解数组与指针
- C++ 关联容器
- C++学习总结
- c语言指针之void
- C++顺序性容器、关联性容器与容器适配器
- 可变参数(C语言):stdarg.h
- C语言预处理及特殊函数介绍(extern/exit/atexit/volatile/static/calloc/realloc等)PART ONE
- C语言指针 指针的赋值,变量的别名
- C语言中排序函数的用法
- C++自带sort函数的用法
- C++中精度的控制