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);

功能:返回子字符串[pos, pos+cout];
当请求的substring超出字符串末尾or count == npos,返回的substring 为[pos, size()]


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM