Answers for "split string in c++ stl"

C++
4

c++ split at character

std::stringstream test("this_is_a_test_string");
std::string segment;
std::vector<std::string> seglist;

while(std::getline(test, segment, '_'))
{
   seglist.push_back(segment); //Spit string at '_' character
}
Posted by: Guest on February-26-2020
1

tokenize string c++

//the program take input as string and delimiter is ','.
//delimiter can  be changed in line 9;

std::vector<std::string> tokenise(const std::string &str){
    std::vector<std::string> tokens;
    int first = 0;
    //std::cout<<"aditya";
    while(first<str.size()){
        int second = str.find_first_of(',',first);
        //first has index of start of token
        //second has index of end of token + 1;
        if(second==std::string::npos){
            second = str.size();
        }
        std::string token = str.substr(first, second-first);
        //axaxax,asas,csdcs,cscds
        //0123456
        tokens.push_back(token);
        first = second + 1;
    }
    return tokens;
}
Posted by: Guest on May-08-2020
2

split a string based on a delimiter in c++

void tokenize(string &str, char delim, vector<string> &out)
{
	size_t start;
	size_t end = 0;

	while ((start = str.find_first_not_of(delim, end)) != string::npos)
	{
		end = str.find(delim, start);
		out.push_back(str.substr(start, end - start));
	}
}

int main()
{
    string s="a;b;c";
    char d=';';
    vector<string> a;
    tokenize(s,d,a);
    for(auto it:a)  cout<<it<<" ";

    return 0;
}
Posted by: Guest on July-21-2020
0

implementing split function in c++

// splits a std::string into vector<string> at a delimiter
vector<string> split(string x, char delim = ' ')
{
    x += delim; //includes a delimiter at the end so last word is also read
    vector<string> splitted;
    string temp = "";
    for (int i = 0; i < x.length(); i++)
    {
        if (x[i] == delim)
        {
            splitted.push_back(temp); //store words in "splitted" vector
            temp = "";
            i++;
        }
        temp += x[i];
    }
    return splitted;
}
Posted by: Guest on June-23-2021
0

split text c++

#include <boost/algorithm/string.hpp>

std::string text = "Let me split this into words";
std::vector<std::string> results;

boost::split(results, text, [](char c){return c == ' ';});
Posted by: Guest on August-03-2021

Browse Popular Code Answers by Language