LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 06-13-2004, 10:57 AM   #1
hylke
Member
 
Registered: Apr 2004
Location: the Netherlands
Distribution: Ubuntu 7.04
Posts: 329

Rep: Reputation: 30
[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.
 
Old 06-13-2004, 12:23 PM   #2
Majjj
LQ Newbie
 
Registered: Sep 2003
Location: Hyderabad
Distribution: Fedora Core 2
Posts: 10

Rep: Reputation: 0
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
 
Old 06-13-2004, 01:03 PM   #3
phlogistonjohn
LQ Newbie
 
Registered: Jul 2003
Location: MA, USA
Distribution: Mandrake,Debian,Zaurus
Posts: 17

Rep: Reputation: 0
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.
 
Old 06-13-2004, 01:15 PM   #4
hylke
Member
 
Registered: Apr 2004
Location: the Netherlands
Distribution: Ubuntu 7.04
Posts: 329

Original Poster
Rep: Reputation: 30
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
 
Old 06-14-2004, 03:37 AM   #5
Majjj
LQ Newbie
 
Registered: Sep 2003
Location: Hyderabad
Distribution: Fedora Core 2
Posts: 10

Rep: Reputation: 0
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.
 
Old 06-14-2004, 11:20 AM   #6
hylke
Member
 
Registered: Apr 2004
Location: the Netherlands
Distribution: Ubuntu 7.04
Posts: 329

Original Poster
Rep: Reputation: 30
Thanx, i finaly understand it
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
search for a string in c alaios Programming 1 07-07-2005 04:50 PM
C....Search a string for a string Scrag Programming 4 06-14-2004 04:15 PM
search for my string alaios Linux - General 6 05-08-2004 01:32 PM
string search in C h/w Programming 17 10-13-2003 06:26 PM
Search for a string TheOriginalH Linux - General 5 09-24-2003 10:48 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration