QtQString字符串分割、截取
在做项⽬中不可避免的会使⽤到⼀串字符串中的⼀段字符,因此常常需要截取字符串。
有两种⽅式可以解决这个问题:
⽅法⼀:QString分割字符串:字符串长度的方法
QString String("yyyy/MM/dd");
QStringList list = date.split("/");//QString字符串分割函数
⽅法⼆:正则表达式分割字符串:
1、Orcale数据库:
1    Data='12345|耗⼦|男'
2select regexp_substr (Data, '[^|]+', 1,1) into ⽤户ID from hdata;
3select regexp_substr (Data, '[^|]+', 1,2) into ⽤户姓名from hdata;
4
5select 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()]

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。