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 13 11670
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.
"sakitah" <sa*********@yahoo.com> wrote in message
news:11*********************@g14g2000cwa.googlegro 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
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
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
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 kb******@gascad.at
"Ludy" <se*******@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr 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
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
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 kb******@gascad.at
what library do i need to include? (because Im getting an error that
it's an undeclared identifier
sakitah wrote: 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.
--
Karl Heinz Buchegger kb******@gascad.at
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.
sakitah wrote: 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
--
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Nick Jacobson |
last post by:
This question is with regard to the * operator as used for sequence
concatenation.
There's the well-known gotcha:
a = ]
b = a*3
b = 4
print b
|
by: Matt Garman |
last post by:
What is the "best" way to copy a vector of strings to an array of
character strings? By "best", I mean most elegantly/tersely written,
but without any sacrifice in performance.
I'm writing an...
|
by: Karl Ebener |
last post by:
Hi!
I am currently using a string to hold data (can be strings as well as
binary!). Now it occured to me, that the <string> might be unable to
handle Null-Bytes. Is that so?
Following...
|
by: Simon Schaap |
last post by:
Hello,
I have encountered a strange problem and I hope you can help me to
understand it. What I want to do is to pass an array of chars to a
function that will split it up (on every location where...
|
by: Shagy |
last post by:
Greetings,
I've been trying to find an equivant c funtion to the c++ copy function.
Description:
copy(char *cstring, size_t count, size_t offset);
Copies "count" characters from a C-style...
|
by: Patrick |
last post by:
Hello
I have the following "easy" problem. I have a string which contains
1000 chars.
Now my task is to cut the string at his 650 position. I tried strcpy,
to copy the first 650 chars into...
|
by: Stephen Russell |
last post by:
I am getting frustrated! I have huge strings in making segments for a SP in
SQL Server. Everything looks good but I want to copy and then past into
ISQL as a test.
this is my error:
Line 1:...
|
by: DogEye |
last post by:
In the C# , I want to change the object only after the user click the
"Submit" button , so I first new an object and use the "=" to get the object
in memory , I found that the operation "=" only...
|
by: Fred |
last post by:
Hi:
I've got the following request: (suppose the required head file
is included)
vector<stringvec;
// ..........
// push some items into vec
string str; // here I want to
copy some range...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |