天天看點

QString 字元串操作

Qt QString字元串分割、截取(轉載)

在做項目中不可避免的會使用到一串字元串中的一段字元,是以常常需要截取字元串。

有兩種方式可以解決這個問題:

方法一:QString分割字元串:

QString date=dateEdit.toString("yyyy/MM/dd");
QStringList list = date.split("/");//QString字元串分割函數      

方法二:正規表達式分割字元串:

1、Orcale資料庫:

1    Data='12345|耗子|男'
2    select regexp_substr (Data, '[^|]+', 1,1) into 使用者ID from hdata;
3    select regexp_substr (Data, '[^|]+', 1,2) into 使用者姓名 from hdata;
4 
5    select regexp_substr (Data, '[^|]+', 1,3) into 性别 from hdata;      

2、

String s = "ab\ncd\nef\\ngh";
String[] v = s.split("[\n]|([\\\\]n)");      

方法三:(字元串截取:QString與std::string均有現成的處理函數)

1)

QString QString::mid(int position, int n = -1) const      

參數:

position:指定截取字元串的起始位置(postion超出字元串長度時,傳回null字元 )

n:指定截取字元串長度(自postion開始的可用字元串小于n,or n== -1,傳回自position開始的全部字元串)

2)

1 std::basic_string::substr(size_type __pos, size_type __n) const
2 basic_string substr(size_type pos = 0, size_type count = npos);