Copy Part of String Into Other Strings

S

sakitah

Hello Everyone,


Here's the problem (I'm using Visual c++ 6.0):


I have a string:
string1 = "This&is&life";


and i want to use the '&' as a seperator to help me parse this string
into 3 strings so that:


string2 = 'This'
string3 = 'is'
string4 = 'life'


thanx
Peace
Sakitah
 
D

Default User

sakitah said:
Hello Everyone,


Here's the problem (I'm using Visual c++ 6.0):


I have a string:
string1 = "This&is&life";

What kind of "string"? A std::string or a C-style array of characters?
The answer tends to be different.



Brian
 
M

Mike Wahler

sakitah said:
Hello Everyone,


Here's the problem (I'm using Visual c++ 6.0):


I have a string:
string1 = "This&is&life";


and i want to use the '&' as a seperator to help me parse this string
into 3 strings so that:


string2 = 'This'
string3 = 'is'
string4 = 'life'
#include <iostream>
#include <sstream>
#include <string>

int main()
{
std::string s1 = "This&is&life";
std::string s2, s3, s4;
std::istringstream iss(s1);

std::getline(iss, s2, '&');
std::getline(iss, s3, '&');
std::getline(iss, s4, '&');

std::cout << s1 << '\n'
<< s2 << '\n'
<< s3 << '\n'
<< s4 << '\n';

return 0;
}

-Mike
 
L

Ludy

Hi Mike,

In case the string is a large one, and we are planning to parse it and
store it in a vector,
how can we get the count of '&' using istringstream so that we can run
a loop to put it in a vector....?
i tired it with string.find but it required the calculation of
positons but its pretty lenghth one.

-Alex
 
D

Dan Cernat

Ludy said:
Hi Mike,

In case the string is a large one, and we are planning to parse it and
store it in a vector,
how can we get the count of '&' using istringstream so that we can run
a loop to put it in a vector....?
i tired it with string.find but it required the calculation of
positons but its pretty lenghth one.

-Alex

one doesn't need to count the '&'. Use a std::vector<string> and
push_back your substrings. It will grow by itself.

Dan
 
K

Karl Heinz Buchegger

Ludy said:
Hi Mike,

In case the string is a large one, and we are planning to parse it and
store it in a vector,
how can we get the count of '&' using istringstream so that we can run
a loop to put it in a vector....?

An istregstream, as the name suggests, works like a stream. That is,
getline() will tell you when there is nothing more to read:

std::string s2;
std::vector< std::string > words;

while( std::getline( iss, s2, '&' ) )
words.push_back( s2 );
 
?

=?iso-8859-1?Q?Ali_=C7ehreli?=

how can we get the count of '&' using istringstream so that we can run
a loop to put it in a vector....?

You don't need to know the number of elements to fill the vector. Just
push_back:

#include <vector>

/* ... */

std::vector<std::string> sections;

while (iss)
{
std::string section;
std::getline(iss, section, '&');

if (iss)
{
sections.push_back(section);
}
}
i tired it with string.find but it required the calculation of
positons but its pretty lenghth one.

If you really need to know, consider the count_if algorithm.

Ali
 
S

sakitah

Thank You Everyone, I have figured it out. Brian, I was using an
std::string.
Basically, I used find_first_of function to return the first instance
of the '&'
Then I used the append function to place what's at the beginning of the
original string1 until the '&' into string2.
Then I used the erase function to delete from the original string from
0 until the first '&' and so on until I filled the rest of the strings.

Now that I did that, I want to deal with the new strings as integers,
how can I do that?
So, say the originals were:

string1 = "001-202-1234567"

and parsing it into:

string2 = "001";
string3 = "202";
string4 = "1234567";

I want to manipulate these as numbers. Sadly, I cant append from the
original string into an integer, so how can I do it?

Thank You
Sakitah
 
K

Karl Heinz Buchegger

sakitah said:
Thank You Everyone, I have figured it out. Brian, I was using an
std::string.
Basically, I used find_first_of function to return the first instance
of the '&'
Then I used the append function to place what's at the beginning of the
original string1 until the '&' into string2.
Then I used the erase function to delete from the original string from
0 until the first '&' and so on until I filled the rest of the strings.

Now that I did that, I want to deal with the new strings as integers,
how can I do that?
So, say the originals were:

string1 = "001-202-1234567"

and parsing it into:

string2 = "001";
string3 = "202";
string4 = "1234567";

I want to manipulate these as numbers. Sadly, I cant append from the
original string into an integer, so how can I do it?

You need to *convert* each string into a number. And again istringstream
does exactly what you want:

int i;
istringstream iss( string1 );
iss > i;

// now i holds the numerical equivalent of the character sequence

As I said in some other posting: istringstream behaves like any other
stream with the only difference that the 'input' comes from a string.
 
S

sakitah

what library do i need to include? (because Im getting an error that
it's an undeclared identifier
 
K

Karl Heinz Buchegger

sakitah said:
what library do i need to include? (because Im getting an error that
it's an undeclared identifier

I don't want to get rude, but:

* do you know how to operate the help system that came with your compiler.

* do you have some books to teach you?

* Do you know how to use google for searching information? You have the worlds
knowledge at your finger tips. Use the force, Lucke.

* Just look up an answer Mike gave on a previous question of yours:

#include <iostream>
#include <sstream>
#include <string>

int main()
{
std::string s1 = "This&is&life";
std::string s2, s3, s4;
std::istringstream iss(s1);

std::getline(iss, s2, '&');
std::getline(iss, s3, '&');
std::getline(iss, s4, '&');

std::cout << s1 << '\n'
<< s2 << '\n'
<< s3 << '\n'
<< s4 << '\n';

return 0;
}

Mike used istringstream and was kind enough to poste a complete program.
 
S

sakitah

If you don't want to get rude, then please don't.
This is afterall one of the resources of the net, and I know the
questions might not seem very complicated for you, but I am under a
very short time limit with my use for the internet. So I try to get as
much as I can. And I do use searches of google and other search
engines.
 
D

Default User

sakitah said:
what library do i need to include? (because Im getting an error that
it's an undeclared identifier

This is a model for an non-informative post. What error? What
identifier does is say is undefined?

Show the code (a complete, minimal program) along with the exact error
messages. Also read my sig below.


Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,233
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top