Split a String
In this short article I want to share a short code about splitting a string like in PHP programming language. As we know in PHP there is a function called explode() to split a string by given delimiter (as single char or substring). For example, given a string str = "the quick brown fox" will be splitted by " " (space char). Simply we just call explode(str, " ") and the functions returns array of string {"the", "quick", "brown", "fox"}.
We can write a "PHP explode()"-like using C++, though the given delimiter is limited to a single char only. Our version of explode() returns std::vector<string> as splitted string.Following is the definition of explode (using C++11):
| |
The code above just a simple function, yet well-tested with various case. Following is the example in main function:
| |
|
will produces output:
the quick brown fox |
This short article my be useful, hopefully.
本文介绍了一个简单的 C++ 函数,该函数模仿 PHP 中的 explode() 函数来按指定的分隔符拆分字符串。文章提供了完整的代码示例及运行效果。
3400

被折叠的 条评论
为什么被折叠?



