Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
06-13-2004, 10:57 AM
|
#1
|
Member
Registered: Apr 2004
Location: the Netherlands
Distribution: Ubuntu 7.04
Posts: 329
Rep:
|
[c++] Search in a string
Hello
I want to read evrything between " and " in a string, how do i do that?
So if i have 'hello "test" another' in a string it puts
'test' in an other string.
Thanks Hylke
Last edited by hylke; 06-13-2004 at 10:58 AM.
|
|
|
06-13-2004, 12:23 PM
|
#2
|
LQ Newbie
Registered: Sep 2003
Location: Hyderabad
Distribution: Fedora Core 2
Posts: 10
Rep:
|
Try this code :-
string s1 = ("hello \"test\" another");
char c = '"';
int a = s1.find(c,0);
int b = s1.find(c,a+1);
if(a<0 || b<0)
cout << "Pattern not found" << endl;
else {
string s2(s1,a+1,b-a-1);
cout << s2 << endl;
}
Now string s2 has the result.
majjj
|
|
|
06-13-2004, 01:03 PM
|
#3
|
LQ Newbie
Registered: Jul 2003
Location: MA, USA
Distribution: Mandrake,Debian,Zaurus
Posts: 17
Rep:
|
I can think of a couple of ways to do it... depends on how robust you want to make it, and how many sub-strings you want to be able to match. Here is a cheezy example I whipped up using a few string.h functions.
Code:
#include <stdio.h>
#include <string.h>
#define MAXSTR 128
#define START 0
#define END 1
int main (void)
{
int index;
char* position [2];
char mystring [MAXSTR] = "this is an \"example\" string\n";
char deststr [MAXSTR];
//initialise the boundry pointers
position[START] = NULL;
position[END] = NULL;
//find index of begin and end quotes
position[START] = strchr( mystring, '"'); //first quote mark
position[END] = strrchr( mystring, '"'); //last quote mark
if( position[START] > 0 && position[END] > 0 &&
position[START] < position[END] )
{
strncpy( deststr, position[START]+1,
position[END]-position[START]-1 );
//since we're only copying part of the string
//we need to fill in the null byte ourselves
deststr[ position[END]-position[START]-1 ] = 0;
}
else
{
printf("Error case\n");
return 1;
}
printf( "OLD STRING: %s\n", mystring );
printf( "NEW STRING: %s\n", deststr );
return 0;
}
I hope you can build off that.
|
|
|
06-13-2004, 01:15 PM
|
#4
|
Member
Registered: Apr 2004
Location: the Netherlands
Distribution: Ubuntu 7.04
Posts: 329
Original Poster
Rep:
|
Thanks all you guys, i'il use majjj's code.
But there's only one thing i don't understand:
Code:
string s2(s1,a+1,b-a-1);
What does it do?
Thanx Hylke
|
|
|
06-14-2004, 03:37 AM
|
#5
|
LQ Newbie
Registered: Sep 2003
Location: Hyderabad
Distribution: Fedora Core 2
Posts: 10
Rep:
|
hi
Code:
string s2(s1,a+1,b-a-1);
s2 (string, start, size)
this means the constructor takes the string (here its s1)and copies the content from start to start+size into s2.
a is the first occurance of "
b is the second
so the size of becomes b-(a+1)
I hope this helps
Majjj
Last edited by Majjj; 06-14-2004 at 03:38 AM.
|
|
|
06-14-2004, 11:20 AM
|
#6
|
Member
Registered: Apr 2004
Location: the Netherlands
Distribution: Ubuntu 7.04
Posts: 329
Original Poster
Rep:
|
Thanx, i finaly understand it 
|
|
|
All times are GMT -5. The time now is 11:13 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|