Strings in C++

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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.
(e-mail address removed)
Remove only "_nos_pam"
 
Hector said:
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";
}
 
Thanx both of you....

--
"The best way to predict the future, is to invent it"


Carl Daniel said:
Hector said:
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";
}
 
Back
Top