Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Copy Part of String Into Other Strings

Reply
Thread Tools

Copy Part of String Into Other Strings

 
 
sakitah
Guest
Posts: n/a
 
      10-04-2005
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

 
Reply With Quote
 
 
 
 
Default User
Guest
Posts: n/a
 
      10-04-2005
sakitah wrote:

> 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
--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      10-05-2005

"sakitah" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> 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


 
Reply With Quote
 
Ludy
Guest
Posts: n/a
 
      10-05-2005
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

 
Reply With Quote
 
Dan Cernat
Guest
Posts: n/a
 
      10-05-2005

Ludy wrote:
> 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

 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      10-05-2005
Ludy wrote:
>
> 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 );

--
Karl Heinz Buchegger
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
=?iso-8859-1?Q?Ali_=C7ehreli?=
Guest
Posts: n/a
 
      10-05-2005
"Ludy" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...

> 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

 
Reply With Quote
 
sakitah
Guest
Posts: n/a
 
      10-05-2005
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

 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      10-05-2005
sakitah wrote:
>
> 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.

--
Karl Heinz Buchegger
(E-Mail Removed)
 
Reply With Quote
 
sakitah
Guest
Posts: n/a
 
      10-05-2005
what library do i need to include? (because Im getting an error that
it's an undeclared identifier

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
what is Deep Copy, shallow copy and bitwises copy.? saxenavaibhav17@gmail.com C++ 26 09-01-2006 09:37 PM
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
is dict.copy() a deep copy or a shallow copy Alex Python 2 09-05-2005 07:01 AM
RAILS: edit turns null strings into empty strings Wybo Dekker Ruby 1 07-23-2005 05:30 PM
Extracting strings delimited by other strings Scott Bass Perl Misc 4 05-12-2005 09:06 PM



Advertisments