简体   繁体   中英

get all the std::string's or first word's before character '='

I wanted to retrieve all the values/words before '='

Ex: 
    A = my first variable, I wanted to get all the strings;
    var2 = I wanted to get this variable also;
    var3 = jcdksjfckjdsckjdscjkdsbckjdsncjsjd; 

Now I wanted to retrieve all the variables (A, var2, var3) from the above text (c++ std::string).

Update: One possible way is

vector<string> myClass::getVariablesFromDescription(string f_description)
{
    vector<string> l_variables;

    stringstream l_desc;
    l_desc << f_description;

    string l_temp;
    string l_prvStr = string();

    for(int i=0; l_desc >> l_temp ; i++ )
    {
        if(l_temp == string("="))
        {
            l_variables.push_back(l_prvStr);
        }

        l_prvStr = l_temp;
    }

    return l_variables;
}

You can create a new string, start copying the original byte-by-byte there and stop when you detect a '='.

string example = "var69 = asdfghjkl";
string var_name;

for(int i=0; i<example.size(); i++)
{
    if(example[i] == '=') break;
    var_name += example[i];
}

I cannot comment yet, but you asked for a hint.

find returns a position of where the = sign is. You also know you started at 0. Then you can get the string from 0 to position and just copy that out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM