In Qt, does one of the containers give me the option to return a comma-separated string from its values?
Answer: Use the split() Method You can use the JavaScript split() method to split a string using a specific separator such as comma ( , ), space, etc. If separator is an empty string, the string is converted to an array of characters.
The QStringList class provides a list of strings. QStringList inherits from QList<QString>. Like QList, QStringList is implicitly shared. It provides fast index-based access as well as fast insertions and removals. Passing string lists as value parameters is both fast and safe.
QStringList::QStringList(const QString &str) Constructs a string list that contains the given string, str. Longer lists are easily created like this: QStringList longerList = (QStringList() << str1 << str2 << str3); See also append().
If your elements are QString
s, you can use QStringList::join()
:
QStringList list;
list << "one" << "two" << "three";
QString s = list.join(",");
// s == "one,two,three"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With