PDA

View Full Version : Get Perticular String from QString



anh5kor
1st April 2016, 06:25
I have a String like below


QString mystring = "0001f0 9f 80 a2 06 bd 40 23 5f 16 8c d8 37 43 fb b4 a4 .....@#_...7C... "
QStringList list = mystring .split(" ");// not working as per expectation

I want to break the string in such a way that my output should be like below


Output
0001f0
9f 80 2a 06 bd 40 23 5f
16 8c d8 37 43 fb b4 a4

Lesiok
1st April 2016, 07:26
What does it mean not working as per expectation ?

anh5kor
1st April 2016, 07:31
I am getting the output as below


list[0] = 0001f0
list[1] = 9f
list[2] = 80
list[3] = 2a
list[4] = 06
list[5] = bd
list[6] = 40
list[7] = 23
list[8] = 5f
list[9] = 16
list[10] = 8c
list[11] = d8
list[12] = 37
list[13] = 43
list[14] = fb
list[15] = b4
list[16] = a4

but my output should be like below


list[0] = 0001f0
list[1] = 9f 80 2a 06 bd 40 23 5f
list[2] = 16 8c d8 37 43 fb b4 a4

Lesiok
1st April 2016, 07:35
You ask to split with a space so what are you surprised ?
Is this string has a fixed format ? If yes just use QString::left, QString::mid and QString::right methods.

d_stranz
1st April 2016, 17:14
Something like this should work, if your lines are always formatted as you show (a 6-byte hex address first, followed by hex as 8 two-byte groups):



QRegularExpression regExp( "^[0-9a-fA-F]6\\s+|(([0-9a-fA-F]2\\s+)8)+");
string.split( regExp, QString::SkipEmptyParts );


Untested, you might have to play around with the regular expression to get it working the way you want. You will probably also have to trim whitespace from the ends of the sub-strings because the regular expression includes the separators as part of the matching.