473,405 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Strip String in 2 blocks

Hello,

I need separete a string in 2 blocks.
For example:
Which is my favourite fruit?|Orange
Which is my favourite color?|Blue
I try to use string.h functions, but always return problems.
My last tentative was:
if ((fp = fopen(filelong,"r"))!=NULL){
while (fgets(sentence,sizeof(sentence),fp)){
asking=strtok(sentence,"|");
//Return "Which is my
favourite fruit?"
}
The question is: How I can take the second period, with the answer??
Thanks!

Apr 10 '06 #1
5 2253
Senger wrote:
I need separete a string in 2 blocks.
For example:
Which is my favourite fruit?|Orange
Which is my favourite color?|Blue
I try to use string.h functions, but always return problems.
My last tentative was:
if ((fp = fopen(filelong,"r"))!=NULL){
while (fgets(sentence,sizeof(sentence),fp)){
asking=strtok(sentence,"|");
//Return "Which is my
favourite fruit?"
}
The question is: How I can take the second period, with the answer??


RTFM on strtok, if you're so attached to it. If not, use 'std::string',
and its members 'find' and 'substr'.

V
--
Please remove capital As from my address when replying by mail
Apr 11 '06 #2
int main()
{
char const str[] = "Which is my favourite fruit?|Orange";

That const shouldn't be there.

-Tomás
Apr 11 '06 #3
Hi Senger,
I need separete a string in 2 blocks.
For example:

Which is my favourite fruit?|Orange
Which is my favourite color?|Blue


you should also have a look at stringstreams: They
can be used to "cut" a string into pieces. Here is
an example:

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

int main()
{
std::istringstream strm("first;second;third");
std::string str;
while(std::getline(strm, str, ';'))
std::cout << str << "\n";
}

And here is the output of this program:

first
second
third

Good luck,
Tilman
Apr 11 '06 #4
Great Solution!
But I still have a problem to convert the str to const char *:

std::istringstream strm(list);
std::string str;
int i=0;
while(std::getline(strm, str, '|')){
i++;
if (i==1){
m_HiddenList.InsertString(-1, str); //Problems... cannot convert
}else{
m_ListFiles.InsertString(-1,str); //Problems... cannot convert
}
}
I try to use a sprintf function, but without sucess...
Do you have any Idea??

Apr 17 '06 #5
Senger wrote:
Great Solution!
But I still have a problem to convert the str to const char *:

std::istringstream strm(list);
std::string str;
int i=0;
while(std::getline(strm, str, '|')){
i++;
if (i==1){
m_HiddenList.InsertString(-1, str); //Problems... cannot convert
}else{
m_ListFiles.InsertString(-1,str); //Problems... cannot convert
}
}
I try to use a sprintf function, but without sucess...
Do you have any Idea??


1. You should really use stringstream.
2. To convert your string to a const char * you can use str.c_str()

Apr 17 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Hank | last post by:
Hi, I have this problem with string.rstrip Python 2.2.3 (#42, May 30 2003, 18:12:08) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import string >>>...
3
by: Brian | last post by:
Hello - has anyone else had trouble with string.lstrip stripping too much? Here's what I get (Python 2.3.4): >>> import string >>> string.lstrip("test/one/two/a.b.c", "test/one/") 'wo/a.b.c'...
14
by: J.S. | last post by:
In a Windows Form application, which is the better method to concatenate large blocks of code? 1. Reading the text from text files. 2. Adding the text to the VB file itself? Thanks! J.S. ...
25
by: electrixnow | last post by:
in MS VC++ Express I need to know how to get from one comma delimited text string to many strings. from this: main_string = "onE,Two,Three , fouR,five, six " to these: string1 =...
2
by: Senger | last post by:
Hello, I need separete a string in 2 blocks. For example: Which is my favourite fruit?|Orange Which is my favourite color?|Blue I try to use string.h functions, but always return problems....
10
by: micklee74 | last post by:
hi if i have a some lines like this a ) "here is first string" b ) "here is string2" c ) "here is string3" When i specify i only want to print the lines that contains "string" ie the first...
4
by: G | last post by:
Hello, Does anyone have any code handy which will take a certain part of a string and save it as an independant string? For example: "Thank you for visiting our web site. You were sent...
7
by: code937 | last post by:
Hey Guys, First time here, i usually resort to google (but for the first time in 3 years he (or she) cant help me) ... Im making some software that generates html reports which can be viewed...
3
by: Colin J. Williams | last post by:
The Library Reference has strip( ) Return a copy of the string with the leading and trailing characters removed. The chars argument is a string specifying the set of characters to be removed....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
Oralloy
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,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.