Qt/QString

Материал из Энциклопедия о программировании
Перейти к: навигация, поиск

QString is a Qt class, that provides a Unicode character string.

Methods

QString trimmed() const
Returns a string that has whitespace removed from the start and the end.
QString simplified() const
Returns a string that has whitespace removed from the start and the end, and that has each sequence of internal whitespace replaced with a single space.
QStringList split(const QString &sep, SplitBehavior behavior = KeepEmptyParts, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
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. (qt qstring split on array)
QString fromStdString(const std::string & str)
Returns a copy of the str string. The given string is converted to Unicode using the fromAscii() function.
QString fromAscii(const char * str, int size = -1)
Returns a QString initialized with the first size characters from the string str.

Examples

Make QString from std string:

std::string str = "Hello world";
QString qstr = QString::fromStdString(str);

If by string mean Ascii encoded const char * then:

const char* str = "Hello world";
QString qstr = QString::fromAscii(str);

QString own implementation of strip:

QString rstrip(const QString& str) {
  int n = str.size() - 1;
  for (; n >= 0; --n) {
    if (!str.at(n).isSpace()) {
      return str.left(n + 1);
    }
  }
  return "";
}

Links