remove any int from a string
-
Hello all, quick question
How can I remove all int numbers from a QString?
example:QString line = ui->comboBox->currenText();
and I get say : Blueberry 2012
I just want to delete the numbers and store Blueberry.
Ive tried:
```
line.removeAt('\t');
line.truncate(9); -
Depends on what exactly you want.
Suppose you have this string:
Total: 6.99
What should be the result?
a) Total: 6.99 (because "6.99" is not an integer when taken as a whole)
b) Total: . (Numbers removed, separator remains)
c) Total: (Whole number including separator removed)You might want to take a look at QString::removeIf or a QString::replace overload that takes a regular expression (any number), in which case you can replace it with nothing.
-
Hello all, quick question
How can I remove all int numbers from a QString?
example:QString line = ui->comboBox->currenText();
and I get say : Blueberry 2012
I just want to delete the numbers and store Blueberry.
Ive tried:
```
line.removeAt('\t');
line.truncate(9);@Ruben12313
As @Asperamanca says. Decide on your rules for what exactly constitutes a "number" or "int" and then use QString &QString::replace(const QRegularExpression &re, const QString &after) to replace your target regular expression for the number/int by an empty string. As an example, to remove all sequences of one or more digits, with an optional preceding minus sign, for "int":QString s = "Blueberry 2012"; s.replace(QRegularExpression("-?\\d+"), "");
In your case you might want to include space character in what you remove, else you'll end up with
"Blueberry "
. -
Hello all, quick question
How can I remove all int numbers from a QString?
example:QString line = ui->comboBox->currenText();
and I get say : Blueberry 2012
I just want to delete the numbers and store Blueberry.
Ive tried:
```
line.removeAt('\t');
line.truncate(9);@Ruben12313 said in remove any int from a string:
I get say : Blueberry 2012
Does the text you get from there always have a similar style like "ABC" + "space/tab" + number?
Then it might be easier to cut after the separator, as you have tried withremoveAt('\t')
and you're done.
Otherwise you have to parse the string without numeric characters (see @JonB 's solution using RegEx)Btw: There are no integers in a string. Numeric characters in a string are interpreted as text string and don't have the numeric value they represent.
"A"
directly converted to int is 65 (or 0x41), while
"65"
as numeric string is 54 53 (or 0x36 0x35)See "ASCII" and "Integer to String conversion".
-
@Ruben12313 said in remove any int from a string:
I get say : Blueberry 2012
Does the text you get from there always have a similar style like "ABC" + "space/tab" + number?
Then it might be easier to cut after the separator, as you have tried withremoveAt('\t')
and you're done.
Otherwise you have to parse the string without numeric characters (see @JonB 's solution using RegEx)Btw: There are no integers in a string. Numeric characters in a string are interpreted as text string and don't have the numeric value they represent.
"A"
directly converted to int is 65 (or 0x41), while
"65"
as numeric string is 54 53 (or 0x36 0x35)See "ASCII" and "Integer to String conversion".
@Pl45m4 yes i only gave blueberry as an example but its a videogame score board:
(playername 5842) when i used removeAT('\t') it did nothing. -
@Pl45m4 yes i only gave blueberry as an example but its a videogame score board:
(playername 5842) when i used removeAT('\t') it did nothing.@Ruben12313 said in remove any int from a string:
when i used removeAT('\t') it did nothing.
Because space (
' '
) is not tab (\t
). -
Hey @Pl45m4 @Asperamanca @JonB
I went for a for loop but got a slight error : conversion from 'int' to 'QChar' is ambiguous
auto numb= line1.length(); for(int i = 0; i < numb; ++i) { if (line1.at(i) == 8 ) { line1.removeAt(i); } }
-
Hi,
Why not search from the end of the string for the first white space character ?
-
@SGaist said in remove any int from a string:
search from the end of the string for the first white space character
Do you have any examples for me to check out? My thing is that in my temp GUI user inputs their name and after the game ends I add the score (lilith k 9876) or (matthew thorn 98739) and I have to update the scores every time they play. So far i can do everything except delete the old score before adding the new one.
-
-
@SGaist solved thanks
QString name = line1; int pos = name.lastIndexOf(QChar(' ')); qDebug() << name.left(pos);
-
-
Depends how generic you want to do it. If you want to remove all numbers from the entire string, something like this should work (although I'm not sure if it will work for big UTF-8 chars):
const auto line = ui->comboBox->currenText(); QString result; for (const auto &character : line) { if (character.isDigit() == false) { result.append(character); } }
-
Depends how generic you want to do it. If you want to remove all numbers from the entire string, something like this should work (although I'm not sure if it will work for big UTF-8 chars):
const auto line = ui->comboBox->currenText(); QString result; for (const auto &character : line) { if (character.isDigit() == false) { result.append(character); } }
-
Oh sorry I was looking at some old version of this post :D Forgot to refresh the window, I didn't notice there are other replies already.
Yes my code will remove all digits in the entire string.