QString则使用隐式共享,又称回写复制。当两个对象共享同一份数据时,数据内容不改变,则不进行数据的复制,即将深拷贝和浅拷贝结合起来使用。
QString使用时在连续的内存块上保存字符串,QString内存分配策略如下:
- 范围0~20:每次4个字符。
- 范围20~4084:每次2倍。
- 范围4084~∞:每次2048个字符。
+:连接字符串
1
2
3
4
|
QString str1 = "I " ; QString str2 = "LOVE " ; QString str3 = "YOU" ; QString str4 = str1 + str2 + str3; //I LOVE YOU |
append():追加字符串
1
2
3
4
|
QString str1 = "Hello " ; QString str2 = "World" ; str1.append(str2); //Hello World str1.append( "!" ); //Hello World ! |
sprintf():赋值/连接/追加字符串
1
2
3
|
QString str; str. sprintf ( "%s" , "Nice to " ); //str = Nice to str. sprintf ( "%s %s" , "Nice to " , "meet you" ); //str = Nice to meet you |
arg():同上
1
2
3
|
QString str; str = QString( "My name is %1, I'm %2 old." ).arg( "Paul" ).arg( "17" ). //str = My name is Paul, I'm 17 old. |
insert():特定位置插入字符串
1
2
3
|
QString str = "This is test code." ; str.insert(8, "not " ); //str = This is not test code. |
prepend():开头插入字符串
1
2
3
|
QString str = "This is test code." ; str.prepend( "Oh! " ); //str = Oh! This is test code. |
replace():替换原字符串某些字符
1
2
3
|
QString str = "This is test code." ; str.replace(13,5, "data." ); //str = This is test data. |
trimmed():移除字符串两端的空白字符
1
2
3
|
QString str = " This is test code. " ; QString str1 = str.trimmed(); //str = This is test code. |
simplified():移除两端空白字符,使用单个空格字符“ ”替代
1
2
3
|
QString str = " This is test code. " ; QString str1 = str.simplified(); //str = " This is test code. " |
startsWith():检查字符串是否以某个字符串开头,Qt::CaseSensitive指定
1
2
3
4
5
6
|
QString str = "This is test code." ; bool result = str.startsWith( "This" ,Qt::CaseSensitive); //result = true QString str = "This is test code." ; bool result = str.startsWith( "is" ,Qt::CaseSensitive); //result = false |
endsWith():功能同上,检查结尾。
1
2
3
4
5
6
|
QString str = "This is test code." ; bool result = str.endsWith( "code." ,Qt::CaseSensitive); //result = true QString str = "This is test code." ; bool result = str.endsWith( "is" ,Qt::CaseSensitive); //result = false |
contains():判断一个字符串是否出现过。
1
2
3
4
5
6
|
QString str = "This is test code." ; bool result = str.contains( "code." ,Qt::CaseSensitive); //result = true QString str = "This is test code." ; bool result = str.contains( "what" ,Qt::CaseSensitive); //result = false |
localeAwareCompare(const QString&, const QString&):比较两个字符串,前小于后返回负值,相等返回0,大于返回正值。此比较时基于平台相关的本地字符集。
1
2
|
qDebug()<< "Result:" <<QString::localeAwareCompare( "good" , "bad" ); //Result: 1 |
compare((const QString&, const QString&,Qt::CaseSensitivity):指定是否进行大小写比较,用法同上。
<:比较是否小于,是则返回true。
<=:比较是否小于等于,是则返回true。
==:比较是否相等,是则返回true。
>=:比较是否大于等于,是则返回true。
>:比较是否大于,是则返回true。
......
toInt():转整型。
toDouble():转双精度浮点型。
toFloat():转浮点型。
toLong():转长整型。
toLongLong():转64位长整型。
......
QByteArray():QString会返回一个const char *的QByteArray,既可以存储原始字节,也可以存储以“\0”结尾的8位字符串。
1
2
3
|
QString str = "I'm good man" ; QByteArray ba = str.toAscii(); ba.append( "yes,very good" ); |
toAscii():返回一个ASCII编码的8位字符串。
toLatin1():返回一个Latin-1(ISO8859-1)编码的8位字符串。
toUtf8():返回一个UTF-8编码的8位字符串,UTF-8是ASCII码的超集,它支持整个Unicode字符集。
toLocal8Bit():返回一个系统本地编码的8位字符串。
isEmpty():检查是否空字符串。
isNull():检查是否为空。
1
2
3
4
|
QString().isNull; //true Qstring().isEmpty(); //true QString( "" ).isNull; //false QString( "" ).isEmpty(); //true |
到此这篇关于Qt QString的使用实现的文章就介绍到这了,更多相关Qt QString内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/baidu_33879812/article/details/123995456