473,385 Members | 1,317 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,385 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 2251
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....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
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...
0
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...
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
1
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...
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...

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.