Possible Duplicate:
How to split a string in C++?
I need to split a string by single spaces and store it into an array of strings. I can achieve this using a istringstream, but what I am not being able to achieve is this:
I want every space to terminate the current word. So, if there are two spaces consecutively, one element of my array should be blank.
For example:
(underscore denotes space)
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.
How can I implement this?
Can you use boost?
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;
}
}
sample execution:
samm$ ./a.out
8 tokens
'hello'
'world'
''
'how'
'are'
''
''
'you'
samm$
If you are not averse to boost, boost.tokenizer is flexible enough to solve this
#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 << "\"\n";
}
int main()
{
split_and_show("This is a string");
split_and_show("This is a string");
}
test: https://ideone.com/mN2sR
You could used simple strtok() function (*)From here. Note that tokens are created on delimiters
#include <stdio.h>
#include <string.h>
int main ()
{
char str[] ="- This is a 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;
}
If strictly one space character is the delimiter,
probably std::getline
will be valid.
For example:
int main() {
using namespace std;
istringstream iss("This is a string");
string s;
while ( getline( iss, s, ' ' ) ) {
printf( "`%s'\n", s.c_str() );
}
}
If you are averse to boost, you can use regular old operator>>
, along with std::noskipws
:
EDIT: updates after testing.
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <algorithm>
#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 << "\n";
}
You could also just use the old fashion 'strtok'
http://www.cplusplus.com/reference/clibrary/cstring/strtok/
Its a bit wonky but doesn't involve using boost (not that boost is a bad thing).
You basically call strtok with the string you want to split and the delimiter (in this case a space) and it will return you a char*.
From the link:
#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;
}
Source: Stackoverflow.com