By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,332 Members | 1,506 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,332 IT Pros & Developers. It's quick & easy.

something like a Split Function

P: n/a
Hello :)

I've got a string in a char* variable. This string is entered by the user
and it can contain some \n

Now I want to split the entered text by this char into different strings.
Exists there any function to do this? The function should return e.g a
char** in which each line is.

Thanks in advance :)
Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Daniel Pomrehn wrote:
Hello :)

I've got a string in a char* variable. This string is entered by the
user and it can contain some \n

Now I want to split the entered text by this char into different
strings. Exists there any function to do this? The function should
return e.g a char** in which each line is.


You could use strtok (not really recommended) or an std::istringstream.
You might also want to look at the tokenizer in the boost library
(though it's probably overkill unless your needs are a bit more complex
than you've described above).

--
Later,
Jerry.

The universe is a figment of its own imagination.

Jul 23 '05 #2

P: n/a
Daniel Pomrehn wrote:
I've got a string in a char* variable. This string is entered by the user
and it can contain some \n

Now I want to split the entered text by this char into different strings.
Exists there any function to do this? The function should return e.g a
char** in which each line is.


In C we used to use 'strtok' for that. I still see it in C++ code from
time to time. Try it, see if it fits.

V
Jul 23 '05 #3

P: n/a
Daniel Pomrehn wrote:
I've got a string in a char* variable. This string is entered by the user
and it can contain some \n
Please put your string in a std::string. This is not the 1970s.
Now I want to split the entered text by this char into different strings.
Exists there any function to do this? The function should return e.g a
char** in which each line is.


std::string test("This%is%a%test");
std::istringstream iss(test);
std::list<string> list;
std::string str;
while (std::getline(iss, str, '%')) {
list.push_back(str);
}

All the std:: things come from headers like <string> and <sstream>. If you
don't know these yet, learn them first before proceding, and your velocity
will be much higher.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces

Jul 23 '05 #4

P: n/a
Daniel Pomrehn wrote:
Hello :)

I've got a string in a char* variable. This string is entered by the user
and it can contain some \n

Now I want to split the entered text by this char into different strings.
Exists there any function to do this? The function should return e.g a
char** in which each line is.

You may use std::find() to find each '\n' and split strings. Assuming a char * string:
#include <vector>
#include <string>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstddef>
std::vector<std::string> SplitString(const char * const cstring)
{
using namespace std;

const size_t SIZE= strlen(cstring);

vector<string> vec;

const char *current, *previous;

for(previous= current= cstring; current!= cstring+SIZE; )
{
current= find(previous, cstring+SIZE, '\n');

vec.push_back( string(previous, current) );

if(current!= cstring+SIZE)
previous= current+1;
}

return vec;
}

int main()
{
using namespace std;

const char *p= "Some\nTest\nString";

vector<string> vec= SplitString(p);

for(vector<string>::size_type i=0; i<vec.size(); ++i)
cout<<vec[i]<<"\n";
}

However this is tricky and error prone. By using a std::string in the first place, it
makes things easier:
#include <vector>
#include <string>
#include <algorithm>
#include <iostream>
std::vector<std::string> SplitString(const std::string &arg)
{
using namespace std;

string &s= const_cast<string &>(arg);

vector<string> vec;

string::iterator current, previous;

for(previous= current= s.begin(); current!= s.end(); )
{
current= find(previous, s.end(), '\n');

vec.push_back( string(previous, current) );

if(current!= s.end())
previous= current+1;
}

return vec;
}

int main()
{
using namespace std;

string s= "Some\nTest\nString";

vector<string> vec= SplitString(s);

for(vector<string>::size_type i=0; i<vec.size(); ++i)
cout<<vec[i]<<"\n";
}


The above involves vector copying, and if this produces overhead, you may do:

#include <vector>
#include <string>
#include <algorithm>
#include <iostream>
void SplitString(std::vector<std::string> &vec, const std::string &arg)
{
using namespace std;

string &s= const_cast<string &>(arg);

string::iterator current, previous;

for(previous= current= s.begin(); current!= s.end(); )
{
current= find(previous, s.end(), '\n');

vec.push_back( string(previous, current) );

if(current!= s.end())
previous= current+1;
}
}

int main()
{
using namespace std;

string s= "Some\nTest\nString";

vector<string> vec;

SplitString(vec, s);

for(vector<string>::size_type i=0; i<vec.size(); ++i)
cout<<vec[i]<<"\n";
}

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.