Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing everything after character (and also character)

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?

like image 482
James Harzs Avatar asked May 01 '12 01:05

James Harzs


2 Answers

I believe that will work.

std::string mystr = string1.substr(0, string1.find("%", 0));
like image 194
Shawn Johnson Avatar answered Oct 23 '22 05:10

Shawn Johnson


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

like image 28
Edward Loper Avatar answered Oct 23 '22 06:10

Edward Loper