remove any int from a string
-
@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".
wrote on 29 Jul 2024, 15:56 last edited by Ruben12313@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.wrote on 29 Jul 2024, 16:17 last edited by Pl45m4@Ruben12313 said in remove any int from a string:
when i used removeAT('\t') it did nothing.
Because space (
' '
) is not tab (\t
). -
wrote on 29 Jul 2024, 18:56 last edited by
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 ?
-
wrote on 29 Jul 2024, 19:16 last edited by
@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.
-
-
wrote on 29 Jul 2024, 20:23 last edited by
@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); } }
@sierdzio this does not take into account player names with numbers in them such as FooBar666 ;-)
-
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.
14/14