Best way to remove leading zeros from a QString
Asked Answered
M

7

7

What do you guys/gals think would be the best way to remove leading zeros from a QString?

I am dealing with numbers like:

099900000002
008800000031
88800000043

Do I have to iterate over every character, one at a time, or is there a more elegant way using the QString::replace() function that I haven't thought of?

Maledict answered 6/1, 2012 at 15:32 Comment(1)
You will have to iterate through the string, directly or indirectly.Rolf
P
15

Remove any number of zeros from the beginning of a string:

myString.remove( QRegExp("^[0]*") );
Passementerie answered 6/1, 2012 at 15:56 Comment(3)
Thanks Chris! I'm still not sure why some people think this is 'complete overkill' but I'm picking this as my answer, and I'll be using it.Maledict
Take myString.remove( QRegExp("^[0]*") ); First you construct a QString, then a QRegExp. Then you do the matching, which will be computationally at least as complicated as my removeLeadingzeros.Talcahuano
I make a habit of not talking about performance until I've measured it. I did some benchmarking and on my machine the RegEx version takes about .012ms per iteration, while your removeLeadingZeros version takes about .00034ms per iteration. While it it definitely way faster, I'd argue that the simplicity and readability of the RegEx outweighs the benefit of the speed of the other function until such time that it becomes a bottleneck. Something that's not likely to happen unless being called thousands and thousands of times a second.Passementerie
T
8

I looked at the QString doc and there is none which will be simple and while being explicit of what you want to do. Just write like this

void removeLeadingzeros(QString &s){

 int i = 0;
 while(i < s.length() && s[i]=='0'){
  i++;
 }

 s.remove(0,i);
}
Talcahuano answered 6/1, 2012 at 16:1 Comment(2)
It's too bad QString doesn't provide something like basic_string::find_first_not_of().Rolf
Why would you use regex for something this simple, performance is why you write c++ right? +1 for this answer. Shame QString doesn't have a trim function which you can feed characters.Barrettbarrette
F
2

Here is an elegant one-liner that doesn't use regex:

while (s.startsWith('0')) { s.remove(0,1); }

Not the fastest, but it is significantly faster than the regex version. Also, if you have C++11, you could do something like this:

s.remove(0,
         std::distance(s.begin(),
                       std::find_if_not(s.begin(), s.end(),
                                        [](QChar c) { return c == '0'; } )));

Which is very fast.

Ferrara answered 7/3, 2013 at 23:17 Comment(1)
These are very nice, but turn sequences of all zeros like "00000000" into empty strings, which is probably not desired behavior. I'd modify the first one to be something like while (s.startsWith('0') && s != "0") { s.remove(0,1); }Nette
S
2

QString mystring = "00000545465651";

mystring = QString::number(mystring.toInt());

// Result = mystring = "545465651";

By converting the QString to an Integer the leading zeros will be removed. Than u just convert it back to a QString.

Slurry answered 19/1, 2021 at 8:24 Comment(0)
B
1

I am not familiar with QStrings, so I am basing this off std::string. Perhaps you can convert pretty simply?

If you can use boost then you could do something like:

std::string s("000003000000300");
boost::trim_left_if( s, boost::is_any_of("0") );
Beaconsfield answered 6/1, 2012 at 15:38 Comment(0)
R
1

If by elegant you mean not iterating yourself, then the fact Qt containers are largely compatible with STL algorithms may be helpful (but not necessary efficient):

QString::iterator n = std::find_if(myQString.begin(), myQString.end(), std::bind2nd(std::not_equal_to<QChar>(), '0'));
myQString.remove(0, n-myQString.begin());

Fancy. But you'd stlll better off iterate yourself as UmNyobe suggested, which is faster and clearer.

Rolf answered 6/1, 2012 at 16:31 Comment(0)
C
0

Assuming myString is a number, or you can find out by checking if ok is true

bool ok = false;
QString::number(myString.toLongLong(&ok));
Chromate answered 8/3, 2016 at 23:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.