Answers for "c++ split string by comma"

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
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

c++ split string by comma into array

#include <vector>
#include <string>
#include <sstream>
#include <iostream>
using namespace std;

int main()
{
    string s="i,love,my,country,very,much";   //declare a string
    string answer[6];  // string array to store the result
    stringstream string_stream(s);  // creating string stream object
	int i=0;            // declaring i and assign  to 0
   
	while(string_stream.good())   // loop will continue if string stream is error free
	{
		string a;       
		getline( string_stream, a, ',' );   //calling getline fuction
		answer[i]=a;
		i++;
	}
   
	for(i=0;i<6;i++)
	{
		cout<<answer[i]<<endl;   // printing a result
	}
    return 0;
}
Posted by: Guest on April-13-2021
0

c++ split string by comma

How to separate a string comma separated
Posted by: Guest on October-03-2021

Browse Popular Code Answers by Language