grab all particular strings in a string.

Hi All I am new to c++ in my work i want to grab a particular string in a stream of string like example..

string str="Source:Registration \n Table:Calls \n Options: \n Calls::date = Payments::PayDate \n \n Source:Registration \n Table:Employee \n Options: \n Employee::Id = Payments::EmpId";

i want to get the Calls::date and Employee::Id with splitting the Payments...

means the particular string before "= Payments::" as this is common..
and what i found is i can get only one "Payments" with the find () method..

Can any one please help me out.. with a sample code...

Thanks in Advance..


I suggest you to read the member function of string library

http://www.cplusplus.com/reference/clibrary/cstring/

since your working with strings.
hello I read that still lot more confusion with logic
Hmm... try using strpbrk() function

1
2
3
4
5
6
7
8
9
10
11
12
char str="Source:Registration \n Table:Calls \n Options: \n Calls::date = Payments::PayDate \n \n Source:Registration \n Table:Employee \n Options: \n Employee::Id = Payments::EmpId";
char anyName[] = "Calls::date";
char * pch;

pch = strpbrk(str, anyName);//pch is a pointer.

//if you want to get the the string after Calls::date, just count the number of characters of
//Calls::date which is 11 plus 1 for the null character '\0'
//getting the string after Calls::date use strncpy() function.
//you must add 11 to pch to get the address of the next character next to "e" <--Calls::date<-
//(pch+11) I think..


I'm not sure if the above code is correct or not. just read the strpbrk() function
http://www.cplusplus.com/reference/clibrary/cstring/strpbrk/
note: your string must be of type char (line 1)
1
2
3
4
5
6
7
8
9
10
11
12
char mystring[]="Source:Registration\nTable:Grid\nOptions:\nGrid::Price <= Emp2::salary\n\nSource:Registration\nTable:Pay2\nOptions:\nPay2::payAmount <= Emp2::salary\nPay2::Empid = Emp2::Id";

char * pch;
char key[]="Emp2::";
  //pch = strpbrk (mystring, key);
  string ala;
  while (pch != NULL)
  {
ala.assign(pch);
cout<<ala;
    pch = strpbrk (pch+6,key);
  }


i tried this but this is not what i want..

I am expecting like this....

string str="Source:Registration \n Table:Calls \n Options: \n Calls::date = Payments::PayDate \n \n Source:Registration \n Table:Employee \n Options: \n Employee::Id = Payments::EmpId";

this is the input string ad i want to split the string with "= Payments::"

The output will be like it will splits three string arrays as
splitted_array[1]="Source:Registration \n Table:Calls \n Options: \n Calls::date = ";
splitted_array[2]="PayDate \n \n Source:Registration \n Table:Employee \n Options: \n Employee::Id ";

splitted_array[3]="EmpId";

as i am splitting the string with the string "= Payments::";

its just like split() function...

can you please help me with a sample code..?

Thanks in advance
Topic archived. No new replies allowed.