QString 擷取分割字串

2021-09-08 12:07:59 字數 842 閱讀 4290

qt中qstring中提供兩種簡單易行的分隔字串的函式,section和split

1.qstring seciton

qstring::section提供了四種過載函式,如下:

qstring    section(qchar sep, int start, int end = -1, sectionflags flags = sectiondefault) const

qstring    section(const qstring & sep, int start, int end = -1, sectionflags flags = sectiondefault) const

qstring    section(const qregexp & reg, int start, int end = -1, sectionflags flags = sectiondefault) const

qstring    section(const qregularexpression & re, int start, int end = -1, sectionflags flags = sectiondefault) const

section入口的第乙個引數為分隔判定符(字元,字串,正規表示式),start為起始位置,end為結束位置,最後乙個為sectionflag.開始和結束位置不是附屬,從左邊開始計數,為0,1,2···,如果是負數就從右邊開始計數,為-1,-2,···,end沒有設定時,預設到字串結束位置。

qchar:

qstring test("one,two,three,four,five");

test=test.section(',&

Qt之QString字串分割 擷取

在做專案中不可避免的會使用到一串字串中的一段字元,因此常常需要擷取字串。有兩種方式可以解決這個問題 方法一 qstring分割字串 qstring date dateedit.tostring yyyy mm dd qstringlist list date.split qstring字串分割函式方...

字串擷取,分割

關於字串擷取有多種方法適應不同的需求 1.substring擷取 string str fakhkajhdkahkdhfakh substring與substrvar str1 str.substring 0,2 從索引為0的位置開始,擷取長度為2位 alert str1 str1 fa eg st...

C 字串分割 擷取

using system using system.collections.generic using system.componentmodel using system.data using system.drawing using system.linq using system.text u...