Right way to split an std::string into a vector<string>

std::split
c++ split string
c++ string> split by space
c++ split string into multiple strings
c++ split string into array
how to divide a string into substrings in c++
c++ split string into array by delimiter
c++ split string by comma

Possible Duplicate: How to split a string?

What is the right way to split a string into a vector of strings. Delimiter is space or comma.

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

How to split a string in C++, In this post I explain the standard and non-standard ways to go about this in C++. be connected to a stream, provided that the right machinery is put in place. std::string text = "Let me split this into words";. std::istringstream iss(text);. std:: vector<std::string> results((std::istream_iterator<std::string>(iss)),. While profiling parts of my code, I noticed that my Split( ) function is rather slow, (about 50% of the time). I was wondering if there was a more efficient way of Splitting a string into a vector.

A convenient way would be boost's string algorithms library.

#include <boost/algorithm/string/classification.hpp> // Include boost::for is_any_of
#include <boost/algorithm/string/split.hpp> // Include for boost::split
// ...

std::vector<std::string> words;
std::string s;
boost::split(words, s, boost::is_any_of(", "), boost::token_compress_on);

C++ Split string into a vector, One way to do this is to use std::copy and std::stringstream : std::vector<std::string > Split(const std::string& subject) { std::istringstream� You have some function that returns a character. You want to then insert the equivalent string into the vector. Assuming your vector is defined as. std::vector<std::string> broken_strings; So you can do it as follows. char ch = ; // Here comes the character that you get from the function. std::string str(1, ch); broken_strings.push_back(str);

vector<string> split(string str, string token){
    vector<string>result;
    while(str.size()){
        int index = str.find(token);
        if(index!=string::npos){
            result.push_back(str.substr(0,index));
            str = str.substr(index+token.size());
            if(str.size()==0)result.push_back(str);
        }else{
            result.push_back(str);
            str = "";
        }
    }
    return result;
}

split("1,2,3",",") ==> ["1","2","3"]

split("1,2,",",") ==> ["1","2",""]

split("1token2token3","token") ==> ["1","2","3"]

Split string in C++ using a delimiter, In this post, we will discuss how to split a string in C++ using a delimiter and construct a vector of strings containing individual strings. std::vector<std::string > &out) Another good alternative is to use std::getline method which extracts� In this article we will see 2 techniques to split a std::string in C++ and return the result in std::vector<std::string> i.e. Splitting a std::string using a char as delimiter. Splitting a std::string using an another std::string as delimiter. How to Split a std::string using a char as delimiter:

You can use getline with delimiter:

string s, tmp; 
stringstream ss(s);
vector<string> words;

while(getline(ss, tmp, ',')){
    words.push_back(tmp);
    .....
}

C++ : How to split a string using String and character as Delimiter , In this article we will see 2 techniques to split a std::string in C++ and return the result in std::vector<std::string> i.e.. Splitting a std::string using a� Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

If the string has both spaces and commas you can use the string class function

found_index = myString.find_first_of(delims_str, begin_index) 

in a loop. Checking for != npos and inserting into a vector. If you prefer old school you can also use C's

strtok() 

method.

Tokenizing a string in C++, Tokenizing a string denotes splitting a string with respect to a delimiter. There are many ways we can tokenize a string. Let's see each of using namespace std;. int main(). {. string vector <string> tokens; We use cookies to ensure you have the best browsing experience on our website. By using our� Active Oldest Votes. This answer is useful. 88. This answer is not useful. Show activity on this post. 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, " "));

How to split a string by space into an array in c++, 1. use std::string's member functions as illustrated by Grzegorz Gołda's answer to How do I split a string by space into In C++ programming, how do you write an array program? How do you shift arrays to the right pointer based using c++?. Best way to split a string in C++? The string can be assumed to be composed of words separated by ; From our guide lines point of view C string functions are not allowed and also Boost is also not allowed to use because of security conecerns open source is not allowed.

4.6. Splitting a String - C++ Cookbook [Book], Splitting a String Problem You want to split a delimited string into multiple strings. std; void split(const string& s, char c, vector<string>& v) { string::size_type i = 0 You have to do this to tell the compiler that it's not reading a right-shift It is easy to use; see Example 4-11 to see how to split a string with Boost's split function. Just so that you know however, it's not the only way. You could also simply copy the contents of the string in to the vector<char>. This is going to be most useful when you either have a vector already instantiated, or if you want to append more data to the end -- or at any point, really. Example, where s is a std::string and v is a std::vector

How to split a string in C++, There is no simple string-splitting method in C++, but there are plenty of ways of doing it. Here are some class Container>. void split2( const std::string& str, Container& cont, char delim = ' ' ) std::vector<std::string> vec;. The input stream that connects to a string, std::istringstream, has an interesting property: its operator>> produces a string going to the next space in the source string.

Comments
  • A split in which commas and spaces are both delimiters, or a function that splits either on space or on comma, according to a parameter?
  • Some of the answers to stackoverflow.com/questions/236129/how-to-split-a-string can readily be adapted to work with multiple delimiters.
  • std::vector<std::string> vstrings(begin, end); would be nicer IMO, but I suppose we don't know whether the questioner is constructing the vector, or hoping to populate a pre-existing vector.
  • Nice, but wrong. The OP was specific in that both space and comma are delimeters. And you can't do the same trick in this case, can you?
  • This is an amazing answer and needs to be highlighted somehow.
  • +1 Very nice. But don't you have to delete the tokens struct created in ss.imbue(std::locale(std::locale(), new tokens())) somewhere?
  • @Yes, I thought the read will do that. Useauto loc = std::make_shared<tokens>(), and then pass ss.imbue(..., loc.get()));. That should work.