QByteArray.split() returning QList with empty first element

Alles rund um die Programmierung mit Qt
Antworten
Freddy_Kay
Beiträge: 5
Registriert: 21. April 2013 23:30

QByteArray.split() returning QList with empty first element

Beitrag von Freddy_Kay »

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: Alles auswählen

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.
Christian81
Beiträge: 7319
Registriert: 26. August 2004 14:11
Wohnort: Bremen
Kontaktdaten:

Re: QByteArray.split() returning QList with empty first elem

Beitrag von Christian81 »

Because the first on is an empty part when you split at '&'. Therefore at least QString has an argument to split():
QString::KeepEmptyParts 0 If a field is empty, keep it in the result.
QString::SkipEmptyParts 1 If a field is empty, don't include it in the result.
MfG Christian

'Funktioniert nicht' ist keine Fehlerbeschreibung
Freddy_Kay
Beiträge: 5
Registriert: 21. April 2013 23:30

Re: QByteArray.split() returning QList with empty first elem

Beitrag von Freddy_Kay »

Christian81 hat geschrieben:Because the first on is an empty part when you split at '&'. Therefore at least QString has an argument to split():
QString::KeepEmptyParts 0 If a field is empty, keep it in the result.
QString::SkipEmptyParts 1 If a field is empty, don't include it in the result.
ich seh grad. das ist ja nen deutsches forum...

Danke dir. Macht jetzt sinn.
Antworten