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

Strings in C++

P: n/a

Well, I have an char* and I need to separe it in pieces... for example I
have this:
"AREA_VERTEX -5.1108 -33.2878 0" and I need to separe this string in an
array of char* with this values...

[0] = "AREA_VERTEX"
[1] = "-5.1108"
[2] = "-33.287"
[3] = "0"

This values are separate by and blank space, please I need any good Idea,
I'm new working with strings in c++

Thnx in advance
--
"The best way to predict the future, is to invent it"
Mar 15 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
> Well, I have an char* and I need to separe it in pieces... for example I
have this:
"AREA_VERTEX -5.1108 -33.2878 0" and I need to separe this string in an
array of char* with this values...

[0] = "AREA_VERTEX"
[1] = "-5.1108"
[2] = "-33.287"
[3] = "0"

This values are separate by and blank space, please I need any good Idea,
I'm new working with strings in c++


Hi,

do you want the pointers in your array to point into the oiginal string, or
do they need to point to new buffers?

to split your string is nt so difficult:
you can use std::stringstream for this:

#include <sstream>
#include <iostream>

....

char str[] = "hello world";
std::stringstream test(str);
string str1;
string str2;
test >> str1;
test >> str2;
cout << str1;
cout << '\n';
cout << str2;

this will split your space separated string into substrings.

another way would be to use the std::string class and manually cut your
string in substrings.
that is more work, but you can do this if your string has a very specific
format that is not easy to
parse with stringstream.

--

Kind regards,
Bruno.
br**********************@hotmail.com
Remove only "_nos_pam"
Mar 15 '06 #2

P: n/a
Hector Y. Martinez wrote:
Well, I have an char* and I need to separe it in pieces... for
example I have this:
"AREA_VERTEX -5.1108 -33.2878 0" and I need to separe this string in
an array of char* with this values...

[0] = "AREA_VERTEX"
[1] = "-5.1108"
[2] = "-33.287"
[3] = "0"

This values are separate by and blank space, please I need any good
Idea, I'm new working with strings in c++


If you're using std::string, here's a simple string splitter (and test
program). This approach is more efficient than using std::stringstream,
with different flexibility tradeoffs. One thing it won't do is skip
consequtive delimitters (e.g. two space) - if there are consequtive
delimitters, you'll get an empty string in the output list.

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

using namespace std;

void split_string(
const string& str,
char delimitter,
vector<string>& ret
)
{
string::size_type delim;
string::size_type prev_delim = 0;

while (string::npos != (delim = str.find_first_of(delimitter,prev_delim)))
{
ret.push_back(str.substr(prev_delim,delim-prev_delim));
prev_delim = delim+1;
}

// Get the tail after the last separator
ret.push_back(str.substr(prev_delim));
}

int main(int argc, char* argv[])
{
if (3 > argc)
{
cerr << "usage: split_test pattern\n";
return 1;
}

vector<string> args;
split_string(argv[1],' ',args);

cout << args.size() << " arguments:\n";
for (vector<string>::iterator it = args.begin(); it != args.end(); ++it)
cout << "'" << *it << "'\n";
}
Mar 15 '06 #3

P: n/a

Thanx both of you....

--
"The best way to predict the future, is to invent it"
"Carl Daniel [VC++ MVP]" wrote:
Hector Y. Martinez wrote:
Well, I have an char* and I need to separe it in pieces... for
example I have this:
"AREA_VERTEX -5.1108 -33.2878 0" and I need to separe this string in
an array of char* with this values...

[0] = "AREA_VERTEX"
[1] = "-5.1108"
[2] = "-33.287"
[3] = "0"

This values are separate by and blank space, please I need any good
Idea, I'm new working with strings in c++


If you're using std::string, here's a simple string splitter (and test
program). This approach is more efficient than using std::stringstream,
with different flexibility tradeoffs. One thing it won't do is skip
consequtive delimitters (e.g. two space) - if there are consequtive
delimitters, you'll get an empty string in the output list.

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

using namespace std;

void split_string(
const string& str,
char delimitter,
vector<string>& ret
)
{
string::size_type delim;
string::size_type prev_delim = 0;

while (string::npos != (delim = str.find_first_of(delimitter,prev_delim)))
{
ret.push_back(str.substr(prev_delim,delim-prev_delim));
prev_delim = delim+1;
}

// Get the tail after the last separator
ret.push_back(str.substr(prev_delim));
}

int main(int argc, char* argv[])
{
if (3 > argc)
{
cerr << "usage: split_test pattern\n";
return 1;
}

vector<string> args;
split_string(argv[1],' ',args);

cout << args.size() << " arguments:\n";
for (vector<string>::iterator it = args.begin(); it != args.end(); ++it)
cout << "'" << *it << "'\n";
}

Mar 15 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.