remove char from qstring
-
Hi,
So you want to remove all zeroes before the first one ?
Is it specifically that or are there other patterns ? -
Hi,
So you want to remove all zeroes before the first one ?
Is it specifically that or are there other patterns ? -
@SGaist yes I would like remove all prefix zeros. so any 0 before 1 or a number/char is removed
@s002wjh
If you really mean these are all numbers which might have leading zeroes you'd like to remove, would it best to convert them from strings to integers, and then write that back to string (if you need to)? Is that what you mean?Otherwise it can be done by regular expression substitution/replacement.
And btw I'm thinking your
so any 0 before 1 or a number/char is removed
means any
0
before any other numeric character/digit,1--9
. As opposed to any character, so if you encountered0A...
you would not want that0
removed, right? -
You can use a simple javascript function like:
function myFunction() { var str = "0101"; var n = str.indexOf("1"); var subString= str.substring(n); console.log("Result", subString) }
The output should be
101
.@GabrielRR
Why would you use JavaScript and how would you call it if you're in Qt (presumably C++) with aQString
? -
@GabrielRR
Why would you use JavaScript and how would you call it if you're in Qt (presumably C++) with aQString
? -
@SGaist yes I would like remove all prefix zeros. so any 0 before 1 or a number/char is removed
@s002wjh You can use https://doc.qt.io/qt-5/qstring.html#remove-5 with a regular expression like (didn't test):
str.remove(QRegularExpression("^0+"));
-
@s002wjh You can use https://doc.qt.io/qt-5/qstring.html#remove-5 with a regular expression like (didn't test):
str.remove(QRegularExpression("^0+"));
-
@s002wjh You can use https://doc.qt.io/qt-5/qstring.html#remove-5 with a regular expression like (didn't test):
str.remove(QRegularExpression("^0+"));
str.remove(QRegularExpression("^0+"));
How come you get away with this, when I am trying to stick with OP's stated requirement:
so any 0 before 1 or a number/char is removed
means any
0
before any other numeric character/digit,1--9
. As opposed to any character, so if you encountered0A...
you would not want that0
removed, right?? :)
-
str.remove(QRegularExpression("^0+"));
How come you get away with this, when I am trying to stick with OP's stated requirement:
so any 0 before 1 or a number/char is removed
means any
0
before any other numeric character/digit,1--9
. As opposed to any character, so if you encountered0A...
you would not want that0
removed, right?? :)
@JonB Well, he can adjust the regular expression for his needs.
Also I don't think your statement is correct: "means any 0 before any other numeric character/digit".
Take a look at his examples and what expected results should be: "001" and "0101", so it become 1 and 101 -
@JonB Well, he can adjust the regular expression for his needs.
Also I don't think your statement is correct: "means any 0 before any other numeric character/digit".
Take a look at his examples and what expected results should be: "001" and "0101", so it become 1 and 101- The point is you can't do your way (
str.remove()
) if you need look ahead/behind :) - I don't know what you mean by "Also I don't think your statement is correct:", I think my statement is correct, you must be mis-interpreting it? Yes, my interpretation would mean
001
->1
and0101
->101
. It would also mean0A
->0A
, which your reg ex remove would not, hence I was asking OP for clarification :)
Chances are OP only ever has
0
followed by digits, only he knows, in which case yours does work, nice and simple. But it's not what he has said/promised so far! - The point is you can't do your way (