Split a String Vector at Whitespace

Right way to split an std::string into a vectorstring

For space separated strings, then you can do this:

std::string s = "What is the right way to split a string into a vector of strings";
std::stringstream ss(s);
std::istream_iterator<std::string> begin(ss);
std::istream_iterator<std::string> end;
std::vector<std::string> vstrings(begin, end);
std::copy(vstrings.begin(), vstrings.end(), std::ostream_iterator<std::string>(std::cout, "\n"));

Output:

What
is
the
right
way
to
split
a
string
into
a
vector
of
strings


string that have both comma and space

struct tokens: std::ctype<char> 
{
tokens(): std::ctype<char>(get_table()) {}

static std::ctype_base::mask const* get_table()
{
typedef std::ctype<char> cctype;
static const cctype::mask *const_rc= cctype::classic_table();

static cctype::mask rc[cctype::table_size];
std::memcpy(rc, const_rc, cctype::table_size * sizeof(cctype::mask));

rc[','] = std::ctype_base::space;
rc[' '] = std::ctype_base::space;
return &rc[0];
}
};

std::string s = "right way, wrong way, correct way";
std::stringstream ss(s);
ss.imbue(std::locale(std::locale(), new tokens()));
std::istream_iterator<std::string> begin(ss);
std::istream_iterator<std::string> end;
std::vector<std::string> vstrings(begin, end);
std::copy(vstrings.begin(), vstrings.end(), std::ostream_iterator<std::string>(std::cout, "\n"));

Output:

right
way
wrong
way
correct
way

Split String to Vector with Whitespaces c++ error

std::cout cannot take a vector, you need to iterate through the container and print each element separately, try using something like this:

int main()
{
string originalString = "Alpha Beta Gamma";

for (const auto& str : split_string(originalString))
cout << str << '\n';

return 0;
}

How to put a string of numbers seperated by whitespaces to an array?

Try boost split (found in #include <boost/algorithm/string.hpp>
), here is an example with tab delimiters:

    string input("hello world"); 
vector<string> result;
boost::split(result, input, ' ');

Alternative just declare a vector and push_back the tokens in the for loop of your code?

Split a string by any number of spaces

Just use strsplit with \\s+ to split on:

x <- "10012      ----      ----      ----      ----       CAB    UNCH       CAB"
x
# [1] "10012 ---- ---- ---- ---- CAB UNCH CAB"
strsplit(x, "\\s+")[[1]]
# [1] "10012" "----" "----" "----" "----" "CAB" "UNCH" "CAB"
length(.Last.value)
# [1] 8

Or, in this case, scan also works:

scan(text = x, what = "")
# Read 8 items
# [1] "10012" "----" "----" "----" "----" "CAB" "UNCH" "CAB"

Split string keeping spaces in R

A possible solution, using stringr:

library(tidyverse)

text_for_column_width = "category variable description value sth"

strings <- text_for_column_width %>%
str_remove("sth$") %>%
str_split("(?<=\\s)(?=\\S)") %>%
unlist

strings

#> [1] "category " "variable " "description "
#> [4] "value "

strings %>% str_count

#> [1] 12 11 17 11

Split string by single spaces

You can even develop your own split function (I know, little old-fashioned):

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();
}

Then you just need to invoke it with a vector<string> as argument:

int main()
{
std::vector<std::string> v;

split( "This is a test", v, ' ' );
dump( cout, v );

return 0;
}

Find the code for splitting a string in IDEone.

Hope this helps.



Related Topics



Leave a reply



Submit