Hey, I'm new to C++, and looking for a way to split a string like you do in C#.

using System;

class Program
{
    static void Main()
    {
        string s = "@This@Is@My@String@";
        string[] words = s.Split("@");
        foreach (string word in words)
        {
            Console.WriteLine(word);
        }
    }
}

A GetBetween Function would work too. Is their anything like this in C++?

Recommended Answers

All 11 Replies

Not literally. But to see what is available you can go to cpprefence.com and look up the methods in what they call C++ String .

cpprefence.com Seems like some Careers site. I can't see any C++ References o_O

Here is C++ version just because I'm feeling nice today.

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <iterator>
#include <sstream>

using namespace std;

template< typename T>
struct EqualCheck{
	T rhs;
	EqualCheck(): rhs(){};
	EqualCheck(const T& Rhs) : rhs(Rhs){}
	bool operator()(const T& arg){ return arg == rhs; }
};
vector<string> split(const string& text,const char sub ){
	string textCopy(text);	
	replace_if(textCopy.begin(),textCopy.end(),EqualCheck<char>(sub),' ');	
	stringstream ss(textCopy);
	vector<string> words;	
	std::copy(istream_iterator<string>(ss),
			  istream_iterator<string>(),
			  back_insert_iterator<vector<string>>(words)
			  );
	return words;
}

template<typename T>
void println(const T& arg){ 
	cout << arg << endl; 
};
int main(){
	std::vector<string> words;
	std::string text = "This@Is@My@String@";	
	words = split(text,'@');
	std::for_each( words.begin(),words.end(),println<string>);

	return 0;
}

I also came up with a verson of this. Not quite as nice as firstPersons but it does the job

#include <iostream>
#include <string>
#include <vector>

void Split(std::string word, std::vector<std::string> & pieces, const char * splitChar)
{
    std::string::const_iterator it = word.begin(), end = word.end();
    std::string temp;
    unsigned int counter = 0;
    while (it != end)
    {
        if (*it != *splitChar)
            temp += *it;
        else
        {
            pieces.push_back(temp);
            temp = "";
        }
        it++;
    }
}

int main()
{
    std::string word = "This@is@a@string@to@split.";
    std::vector<std::string> pieces;
    Split(word, pieces, "@");
    for (size_t i = 0; i < pieces.size(); i++)
    {
        cout << pieces[i] << endl;
    }
    cin.get();
    return 0;
}

Idk, maybe its just me, but personally, I like returning the result, instead of it
being an argument is better. It feels like C++ is more return result, and C is more write result
into the destination provided.

Well the reason I have the vector in the function parameters is that until I looked at your post more closely I wasn't sure if you could return a vector defined in a function. I try not to return something that was defined in a function out of habit for some reason. Can't remember anymore why that is though.

Oops, there is a major bug in my program. Say the call was :

std::vector<string> words;
std::string text = "This, is a string, to be dealt with;
words = split(text,',');

The problem is that it will split by both, space and comma. So disregard all my code and use this :

vector<string> split(const string& text,const char sub ){
	stringstream ss(text);
	vector<string> words;	
	string tmp;
	while(ss && getline(ss,tmp,sub)){ words.push_back(tmp);}
	return words;
}

It's not necessary to create your own split function, as boost has one and even foreach if you want.

#include <iostream>
#include <string>
#include <vector>
#include <boost/algorithm/string.hpp>
#include <boost/foreach.hpp>

using namespace std;
using namespace boost;
#define foreach BOOST_FOREACH

int main()
{
   string s = "@This@Is@My@String@";
   vector<string> parts;
   split(parts,s,is_any_of("@"));
   foreach(string& p,parts)cout << p << endl;
}
Member Avatar for iamthwee
#include <iostream>
#include <string>
#include <sstream>
#include <vector>

using namespace std;

int main()
{
   vector<string>array; 
   string token;
   string tmp = "this@is@a@line";

   istringstream iss(tmp);

   while ( getline(iss, token, '@') )
   {
     array.push_back(token);
   }
   
   cout << array[1];
   
   cin.get();
}

Or something a bit more easy:

#include <iostream>
#include <string> 
#include <vector> 
#include <sstream>
 
int main()
{
    std::stringstream s_str("@This@Is@My@String@");
    std::vector<std::string> words;
    std::string word = "";
    while (std::getline(s_str, word, '@')) 
        words.push_back(word);
    
    for (size_t s = 0; s != words.size(); ++s)
        std::cout << words.at(s) << '\n';
}

[edit]
cross-post with Iamtwee..

>>cout << array[1];

What's that supposed to do?

Member Avatar for iamthwee

>>cout << array[1];
What's that supposed to do?

Nothing really, just prove it has been split up...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.