15

I'm in search for an easy and foolproof way to convert an arbitrary QStringList to a single QString and back.

QStringList fruits;
fruits << "Banana", "Apple", "Orange";
QString packedFruits = pack(fruits);
QStringList unpackFruits = unpack(packedFruits);

// Should be true 
// fruits == unpackFruits;

What might be the easiest solution for this kind of problem?

LogicStuff
  • 19,397
  • 6
  • 54
  • 74
Aleph0
  • 5,816
  • 4
  • 29
  • 80

3 Answers3

32

From QStringList to QString - QStringList::join:

Joins all the string list's strings into a single string with each element separated by the given separator (which can be an empty string).

QString pack(QStringList const& list)
{
    return list.join(reserved_separator);
}

From QString to QStringList - QString::split:

Splits the string into substrings wherever sep occurs, and returns the list of those strings. If sep does not match anywhere in the string, split() returns a single-element list containing this string.

QStringList unpack(QString const& string)
{
    return string.split(reserved_separator);
}
LogicStuff
  • 19,397
  • 6
  • 54
  • 74
  • Thanks for the fast reply. Maybe, I was a little bit inaccurate. I'm aware of the `join` and `split` functions. But what if any of the strings already contains the separator. Then I'm failing. – Aleph0 May 31 '16 at 07:11
  • This is the use-case... you must pick a unique separator. – LogicStuff May 31 '16 at 07:13
  • 1
    Seems to be. As the QStringList is entered by the user, it seems that I have to explicitly forbid a special character. – Aleph0 May 31 '16 at 07:25
  • Or you can use an nonprintable character as a seperator. like \1f (US). Or pick another one from the ascii charts from https://en.wikipedia.org/wiki/ASCII – 0rko May 31 '16 at 07:27
7

Previous answers mentioned QString::split and QStringList::join which is the correct way, but if the separator you choose is included in any of the strings it will break your conversion.

You must prevent strings in the list to contain your separator with one of the following techniques:

  • Throw an error before QStringList::join if any string includes the separator
  • Ensure they can not contain the separator (for example storing the string with its QByteArray::toHex(myString.toLatin1()) representation, then using a separator that has character(s) outside of the range 0..9 and a..f. Then convert back with QString::fromLatin1(QByteArray::fromHex(myHexString)) afterward
  • Use any separator regardless if the strings contain it, but implement an escape logic for it before the join(), and an un-escape logic after the split(), so that the separator is never present in any of the strings at the time of join, but all instances of it will be restored.
FPGA warrior
  • 241
  • 3
  • 11
6

Use QStringList::join() :

QStringList strList;
strList << "Banana" << "Apple" << "Orange" ;

QString str = strList.join("");  // str = "BananaAppleOrange";
str = strList.join(",");  // str = "Banana,Apple,Orange";
IAmInPLS
  • 4,051
  • 4
  • 24
  • 57