I want to remove leading zeroes from string like "0000000057"
.
I did like this but didn't get any result:
string AccProcPeriNum = strCustData.substr(pos, 13);
string p48Z03 = AccProcPeriNum.substr(3, 10);
I want output only 57.
Any idea in C++?
#include <string>
std::string str = "0000000057";
str.erase(0, str.find_first_not_of('0'));
assert(str == "57");
LIVE DEMO
Piotr S's answer is good but there is one case it will return the wrong answer, that is the all-zero case:
000000000000
To consider this in, use:
str.erase(0, min(str.find_first_not_of('0'), str.size()-1));
Even when str.size()
will be 0
, it will also work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With