Split string by single spaces
Possible Duplicate:
How to split a string in C++?
我需要将一个字符串按单个空格拆分,并将其存储到一个字符串数组中。我可以使用IStringstream实现这一点,但我无法实现的是:
我希望每个空格都结束当前单词。所以,如果连续有两个空格,那么数组中的一个元素应该是空白的。
例如:
(下划线表示空格)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | This_is_a_string. gets split into: A[0] = This A[1] = is A[2] = a A[3] = string. This__is_a_string. gets split into: A[0] = This A[1] ="" A[2] = is A[3] = a A[4] = string. |
我如何实现这一点?
如果一个字符的空间完全是delimiter;
1 2 3 4 5 6 7 8 9 | int main() { using namespace std; istringstream iss("This is a string"); string s; while ( getline( iss, s, ' ' ) ) { printf("`%s' ", s.c_str() ); } } |
你可以发展你的个性,甚至分裂的功能(知道了,小老啤酒): </P >
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | size_t split(const std::string &txt, std::vector<std::string> &strs, char ch) { size_t pos = txt.find( ch ); size_t initialPos = 0; strs.clear(); // Decompose statement while( pos != std::string::npos ) { strs.push_back( txt.substr( initialPos, pos - initialPos ) ); initialPos = pos + 1; pos = txt.find( ch, initialPos ); } // Add the last one strs.push_back( txt.substr( initialPos, std::min( pos, txt.size() ) - initialPos + 1 ) ); return strs.size(); } |
然后你就要退化和一个矢量信息<字符串>作为论据: </P >
1 2 3 4 5 6 7 8 9 | int main() { std::vector<std::string> v; split("This is a test", v, ' ' ); dump( cout, v ); return 0; } |
find the code for ideone劈(字符串输入。 </P >
希望这helps。 </P >
你能使用升压? </P >
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | samm$ cat split.cc #include <boost/algorithm/string/classification.hpp> #include <boost/algorithm/string/split.hpp> #include <boost/foreach.hpp> #include <iostream> #include <string> #include <vector> int main() { std::string split_me("hello world how are you" ); typedef std::vector<std::string> Tokens; Tokens tokens; boost::split( tokens, split_me, boost::is_any_of("") ); std::cout << tokens.size() <<" tokens" << std::endl; BOOST_FOREACH( const std::string& i, tokens ) { std::cout <<"'" << i <<"'" << std::endl; } } |
样本的执行: </P >
1 2 3 4 5 6 7 8 9 10 11 | samm$ ./a.out 8 tokens 'hello' 'world' '' 'how' 'are' '' '' 'you' samm$ |
如果你是averse到升压,你可以使用正则
编辑:更新后的测试。 </P >
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | #include <iostream> #include <iomanip> #include <vector> #include <string> #include #include <iterator> #include <sstream> void split(const std::string& str, std::vector<std::string>& v) { std::stringstream ss(str); ss >> std::noskipws; std::string field; char ws_delim; while(1) { if( ss >> field ) v.push_back(field); else if (ss.eof()) break; else v.push_back(std::string()); ss.clear(); ss >> ws_delim; } } int main() { std::vector<std::string> v; split("hello world how are you", v); std::copy(v.begin(), v.end(), std::ostream_iterator<std::string>(std::cout,"-")); std::cout <<" "; } |
http:/ / / 62mcc ideone.com </P >
你可能也在使用的"老"的strtok服装 </P >
www.cplusplus.com http:/ / / / /参考clibrary CString strtok / / </P >
它的一块wonky但不涉及到使用升压(Boost注释,冰浴(的事)。 </P >
你基本上呼叫strtok与字符串,你想分裂和delimiter(在这个案例A空间)和它会回报你一个char *。 </P >
从链接: </P >
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include <stdio.h> #include <string.h> int main () { char str[] ="- This, a sample string."; char * pch; printf ("Splitting string "%s" into tokens: ",str); pch = strtok (str," ,.-"); while (pch != NULL) { printf ("%s ",pch); pch = strtok (NULL," ,.-"); } return 0; } |
如果你注意到averse升压,boost.tokenizer冰足够灵活的解决这 </P >
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include <string> #include <iostream> #include <boost/tokenizer.hpp> void split_and_show(const std::string s) { boost::char_separator<char> sep("","", boost::keep_empty_tokens); boost::tokenizer<boost::char_separator<char> > tok(s, sep); for(auto i = tok.begin(); i!=tok.end(); ++i) std::cout << '"' << *i <<"" "; } int main() { split_and_show("This is a string"); split_and_show("This is a string"); } |
测试:http:/ / / mn2sr ideone.com </P >
你可以用简单的请求strtok()函数时函数(*)从这里。注意这是对创新tokens分隔符。 </P >
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include <stdio.h> #include <string.h> int main () { char str[] ="- This is a string"; char * pch; printf ("Splitting string "%s" into tokens: ",str); pch = strtok (str," ,.-"); while (pch != NULL) { printf ("%s ",pch); pch = strtok (NULL," ,.-"); } return 0; } |