QByteArray.split() returning QList with empty first element
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".
Code:
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.
Re: QByteArray.split() returning QList with empty first element
As a special character is treated as a marker for the end of the element. String "&&&aaa" gives You 4 elements.
Re: QByteArray.split() returning QList with empty first element
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).
Re: QByteArray.split() returning QList with empty first element
right. Gotcha. Thank you!
Re: QByteArray.split() returning QList with empty first element
Take a look at the other split() arguments if you want to suppress empty sections.
Re: QByteArray.split() returning QList with empty first element
Quote:
Originally Posted by
ChrisW67
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.
Re: QByteArray.split() returning QList with empty first element
Having read the thread again I think the OP should use regular expressions rather than split.
Re: QByteArray.split() returning QList with empty first element
Quote:
Originally Posted by
wysota
Having read the thread again I think the OP should use regular expressions rather than split.
And why is that?
Re: QByteArray.split() returning QList with empty first element
Because you want to "distinguish" things rather than split columns. At least that's what I understood.
Re: QByteArray.split() returning QList with empty first element
Quote:
Originally Posted by
wysota
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 ;)
Re: QByteArray.split() returning QList with empty first element
Yes, you can still use regular expressions.