View Full Version : QByteArray.split() returning QList with empty first element
FreddyKay
4th February 2015, 11:41
Hey Guys,
I got a QByteArray containing some data. I distinguish different kinds of data via key characters which I prepend with a '&' character. Currently the data QByteArray only contians something like this: data = "&t02.04.2014".
void foo(QByteArray data){
QList<QByteArray> cmdList = data.split('&');
if(cmdList.size() != data.count('&')) dispError("Command List size != number of '&'");
//At this point the cmdList contains 2 elments where the first one is empty (presuming the data array is like noted above.
//do fancy stuff with the data
}
I don't quite understand why there are two elments in the QList as I am only expecting one.
Lesiok
4th February 2015, 13:08
As a special character is treated as a marker for the end of the element. String "&&&aaa" gives You 4 elements.
wysota
4th February 2015, 14:10
In other words "&" separates items. Therefore by definition the number of items in the array equals to number of separators + 1. Since you have one occurence of the separator, you have two items (no separators would yield one item -- the whole input string).
FreddyKay
4th February 2015, 14:38
right. Gotcha. Thank you!
ChrisW67
4th February 2015, 20:23
Take a look at the other split() arguments if you want to suppress empty sections.
Lesiok
5th February 2015, 06:54
Take a look at the other split() arguments if you want to suppress empty sections.
QByteArray::split don't have other arguments, QString::split has them.
wysota
5th February 2015, 07:26
Having read the thread again I think the OP should use regular expressions rather than split.
FreddyKay
5th February 2015, 13:33
Having read the thread again I think the OP should use regular expressions rather than split.
And why is that?
wysota
5th February 2015, 15:57
Because you want to "distinguish" things rather than split columns. At least that's what I understood.
FreddyKay
6th February 2015, 10:43
Because you want to "distinguish" things rather than split columns. At least that's what I understood.
What I ultimatelly will have will be Array like this: [&t25.01.2015&satFancy Name//6000//Blablubb//something//42&antRunning//0//Illuminati&gs6300//2655].
Each of the & indicates that the following stuff represents some values for objects that are specified by th name that follows '&'. The '//' seperate different values. Would it still be possible to use Regular Expressions? From first glance I figured split was what I wanted but you can teach me better ;)
wysota
6th February 2015, 12:41
Yes, you can still use regular expressions.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.