Extract all particular strings by splitting 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..
Hi farooq,

I assume that the string you might be getting is in proper format, means to say the information are seperated by delimiter.

this is the sample code, hope this helps
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<iostream>
#include<string>
#include<stdlib.h>
using namespace std;
int main()
{
	std::string  str="EmployeeId=1234;EmployeeName=Som;EmployeeSalary=123456;";
	//Delimiter is semicolon
	std::string reqString;
	int iPos = str.find(";");
/*
npos is a static member constant value with the greatest possible value for an element of type size_t.
This value, when used as the value for a count parameter n in string's member functions, roughly indicates "as many as possible".
hen used in some pos parameters that allow for out-of-range values, npos indicates the end of the string.
As a return value it is usually used to indicate failure.

*/
	while(iPos != string::npos)
	{
		 reqString = str.substr(0/*intial position*/,iPos);
		 cout<<reqString.c_str()<<endl;
		 //if you dont want to erase keep a copy of the string
		 str.erase(0,iPos+1);
		  iPos = str.find(";");
	}

	
	return 0;
}
Last edited on
Hello Somshekher,

Yes this is good for the single character splitting but i need to split with the given word..
here the word is

= Payments::

so could you please tell me how can i do that..????
Thanks in Advance.

Regards,
Farooq.
What kind of out put are you expecting given the input you have written above?

I expect that you are trying to do this:

INPUT from whatever source

"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"

OUTPUT to what ever

Source:Registration \n Table:Calls \n Options: \n Calls::date //Goes to either a stream of variable.

= Payments::PayDate \n \n Source:Registration \n Table:Employee \n Options: \n Employee::Id = Payments::EmpId //Goes elsewhere or is thrown out.

Is this correct?
Hello ,

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...



hi farooq,
sorry was busy so dint replied.Anyways you can use the same logic what i have used.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
#include<string>
#include<stdlib.h>
using namespace std;
int main()
{
	std::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";
	int iPos = str.find("= Payments::");
std::string sDelimiterString ="= Payments::";
int iNumOfChars = sDelimiterString .size();
	std::string reqString;
while(iPos != string::npos)
	{
		 reqString = str.substr(0/*intial position*/,iPos);
		 cout<<reqString.c_str()<<endl;
		 //if you dont want to erase keep a copy of the string
		 str.erase(0,iPos+iNumOfChars );
		  iPos = str.find("= Payments::");
	}

}




Output is

1
2
3
4
5
6
7
8
9
10
Source:Registration
 Table:Calls
 Options:
 Calls::date
PayDate

 Source:Registration
 Table:Employee
 Options:
 Employee::Id
Last edited on
Topic archived. No new replies allowed.