miladjurablu
miladjurablu

Reputation: 91

How to parse string and get the specific part of string QT

this is my first project in Qt

I have project in Qt and I have a string look like this

"0044RQ039PR006000000AM009123456789CU003364PD0011"

how can I get the number after AM like this

QString firstThreeNumbers = "009"     //first 3 number's

QString restOfNumbers = "123456789"   //rest of number's

the position of the AM is dynamic, it's maybe look like this

"AM0091234567890044RQ039PR006000000CU003364PD0011"

Upvotes: 0

Views: 327

Answers (2)

ben10
ben10

Reputation: 241

How about a regex?

(AM)([0-9]{3})([0-9]*)

The first group is your AM, then 3 mandatory digits, then the rest of your digits. You'd have 3 important groups + the full match.

You can play around with it here

seems like it can be used with QString

Upvotes: 0

Marco Beninca
Marco Beninca

Reputation: 615

Maybe something like the following will work

QString str = "0044RQ039PR006000000AM009123456789CU003364PD0011";

int index = str.IndexOf("AM");
if (index >= 0)
{
    QString number;
    index += 2;
    while (index < str.size() && str[index].isDigit())
    {
        number += str[index];
    }
}

Upvotes: 1

Related Questions