can any1 split the input string

Hi All,

I am new to c++, I want to split the string and add some thing to it
for example let say iam getting a input

string old="company$45$0.07$29-09-2009$company2$34$0.09$21-09-2009"

now i want a output where it should come like

string outnew=" ['company',45,0.07,'29-09-2009'],['company2',34,0.09,'21-09-2009'] ";

where i am splitting the string with $ char.

Please help me out.

Thanks in Advance

Regards,

Farooq.
use std::string::find() to find the first $. use std::string::substr() to extract the portion of the string
from the beginning up to the $. use std::string::substr() to "crop" off that beginning portion.
Repeat to get the next field, etc.
I usually use the following function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
inline std::vector < std::string > split(const std::string &s, const char *by = " ")
{
    std::vector < std::string > res;
    res.reserve(2);
    const unsigned int SIZE = s.size();
    unsigned int i(0);
    unsigned int j(0);
    for (i = 0; i <= SIZE; i = j + 1)
    {
        for (j = i; j < SIZE && strchr(by, s[j]) == NULL; j++)
        {;}
        res.push_back(s.substr(i, j-i));
    }
     return res;
}

in your case "by" param should be "$"
And of course you need to include required headers files
What's wront std::string::find_first_of()? Or hell, even strpbrk().
Last edited on
Thank you Denis its worked fine..

Can you help me in splitting the given string with a string?

I mean 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...
Topic archived. No new replies allowed.