Removing everything after character (and also character)

25,659

Solution 1

I believe that will work.

std::string mystr = string1.substr(0, string1.find("%", 0));

Solution 2

std::string the_prefix_you_want = string1.substr(0, string1.find("%"));

See: http://www.cplusplus.com/reference/string/string/find/ and http://www.cplusplus.com/reference/string/string/substr/ for more details

Share:
25,659
James Harzs
Author by

James Harzs

Updated on March 24, 2020

Comments

  • James Harzs
    James Harzs about 4 years

    I have a string like this:

    std::string string1 = "xjdfhfakdjs%54k34k.-jk34";
    

    I need to get only ""xjdfhfakdjs", but the string is dynamic, not hardcoded so I don't know what is it, the length etc. so I wanted to remove everything after %, and also the % char.

    How could I do this?