dimanche 21 janvier 2018

how to get a number from a string using only std::string?

i have a function that accepts two strings , "first" and "second" i want to check if the second string is in this format : "first"_number , so i can retuurn the number in the second string

for example if the first string is hello and the second string is hello_7 , then i want to return 7 .. if the string is not in this format i return 0 ..

any help ? i can only use string std .. if htere is no other way then i can use other std functions.

int Plain::getNumber(string first, string second){
    std::size_t pos = second.find("_");
    std::string str1 = str.substr (0,pos);
    if(str1  ==  first){
    // here i want to get the number
    }
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire