qt QString的Split方法(字符串被某个字符拆分成集合)

qt QString的Split方法(字符串被某个字符拆分成集合)

今天想在数据库存储一组数据,因为数组的长度不是固定的,所以想存的时候是否可以存字符串,然后用固定的“,”隔开存储,取值的时候再使用split(",")取值,尝试后可取,代码如下:

1
2
3
4
5
6
7
8
9
10
void MainWindow::strSplit()
{
    QString str = "0,1,2,3,4,5,6,7,8,9";
    QStringList  strs=  str.split(",");
    foreach (QString s, strs)
    {
       int n =  s.toFloat();
       qDebug() << n;
    }
}

按F1后查看帮助:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.
cs specifies whether sep should be matched case sensitively or case insensitively.
If behavior is QString::SkipEmptyParts, empty entries don't appear in the result. By default, empty entries are kept.
Example:

  QString str = "a,,b,c";

  QStringList list1 = str.split(',');
  // list1: [ "a", "", "b", "c" ]

  QStringList list2 = str.split(',', QString::SkipEmptyParts);
  // list2: [ "a", "b", "c" ]

See also QStringList::join() and section().

希望可以帮助 , 谢谢观看~~