Qstring

原地址:https://blog.youkuaiyun.com/qq_40194498/article/details/79629632

QString在Qt的各种数据转换中可谓举足轻重,熟悉QString的用法对于Qt编程真的是如虎添翼:



QString 字符串有相当多的操作符和函数,此处只介绍一些比较常用的:


(1) “+” 用于组合两个字符串,“+=” 用于将一个字符串追加到另一个字符串的末尾,例如:


 QString str1 = "Welcome";
 str1 = str1 + "to you !";   //str1 = "Welcome to you!"
 QString str2 = "Hello,";
 str2 += "World!";      //str2 = "Hello,World!"
1
2
3
4
(2)QString::append()函数,具有与“+=”操作符同样的功能,实现字符串末尾追加另一个字符串,例如:


 QString str1 = "Welcome ";
 QString str2 = "to ";
 str1.append(str2);             //str1 = "Welcome to "
 str1.append("you !");         //str1 = "Welcome to you !" 
1
2
3
4
(3)组合字符串的另一个函数是QString::sprintf(),此函数支持的格式定义和C++库中的函数sprintf()定义一样,例如:


 QString str;
 str.sprintf("%s","Welcome ");     //str = "Welcome "
 str.sprintf("%s"," to you! ");      //str = " to you! "
 str.sprintf("%s %s","Welcome "," to you! ");     //str = "Welcome  to you! "; 
1
2
3
4
(4)Qt还提供了另一种方便的字符串组合方式,使用QString::arg()函数,此函数的重载可以处理很多的数据类型。此外,一些重载具有额外的参数对字段的宽度、数字基数或者浮点精度进行控制。相对于QString::sprintf(),QString::arg()是一个比较好的解决方案,因为它类型安全,完全支持Unicode,并且允许改变“/n”参数的顺序。例如:


 QString str;
 str = QString("%1 was born in %2.").arg("Joy").arg(1993);     //str =  "Joy was born in 1993.";
1
2
其中:


  “%1” 被替换为“Joy”.


  “%2”被替换为“1993”.


(5)QString 也提供了一些其他的组合字符串的方法,包括以下几种:


  a. insert()函数:在原字符串特定位置插入另一个字符


  b. prepend()函数:在原字符串开头插入另一个字符串


  c. replace()函数:用指定的字符串去代替原字符串中的某些字符


(6)去除字符串两端的空白(空白字符包括回车符号“\n”、换行符“\r”、制表符”\t”和空格字符:“ ”等)非常常用,如获取用户输入账号时就需要去掉空白符。


  a. QString::trimmed()函数:移除字符串两端的空白符


  b. QString::simplified()函数:移除字符串两端的空白字符,使用单个空格字符“ ”代替字符串中出现的空白字符。


例如:


 QString str1 = "   Welcome  \t  to  \n  you!   ";
 QString str2 = "   Welcome  \t  to  \n  you!   ";
 str1 = str1.trimmed();      // str1 = " Welcome \t to \n you! "
 str2 = str2.simplified();     // str2 = " Welcome to you ! " 
1
2
3
4
(7)split函数用于拆分字符串


    QByteArray array = tcpSocket->readAll();//从套接字读取数据
    QString strr = array;//将数据转换为QString格式
    QStringList strlist=strr.split(" ");//将数据以空格拆分,并放入数组中
    QStringList list=strlist.at(1).split("#");//将strlist中的第一号元素以“#”隔开并存入list中
    qDebug()<<list.at(0);//打印出list中的首个元素
1
2
3
4
5
(8)clear用于清除字符串


str = "hello"; 
str.clear();//此后字符串为空
1
2
(9)indexOf函数用于在一个字符串中查找另一个字符串,返回第一次出现的索引位置,如果没有找到,返回-1。


QString x = "sticky question";
QString y = "sti";
x.indexOf(y);               // returns 0
x.indexOf(y, 1);            // returns 10
x.indexOf(y, 10);           // returns 10
x.indexOf(y, 11);           // returns -1
1
2
3
4
5
6
(10)fill函数用于初始化字符串或给字符串赋值,将字符串中的每个字符设置为字符ch。如果字符串大小与(默认)不同,那么字符串就会预先调整大小。


QString str = "Berlin";
str.fill('z');
// str == "zzzzzz"


str.fill('A', 2);
// str == "AA"
1
2
3
4
5
6
(11) insert函数用于在给定的索引位置插入字符串str,并返回对该字符串的引用。


QString str = "Meal";
str.insert(1, QString("ontr"));
// str == "Montreal"
1
2
3
(12) isEmpty函数用于判断字符串是否为空,并返回判断结果


QString().isEmpty();            // returns true
QString("").isEmpty();          // returns true
QString("x").isEmpty();         // returns false
QString("abc").isEmpty();       // returns false 
1
2
3
4
(13) isNull效果和 isEmpty类似,如果该字符串为空,则返回true;否则返回false。


QString().isNull();             // returns true
QString("").isNull();           // returns false
QString("abc").isNull();        // returns false 
1
2
3
(14)length函数返回该字符串中的字符数。相当于size()。 
(15)resize(size)函数用于将字符串的大小设置为size大小。 
如果字符串长度大于当前长度,则将字符串扩展为size长度长,并将额外的字符添加到末尾,新字符未初始化。 
如果大小小于当前大小,则从末尾删除字符。


QString s = "Hello world";
s.resize(5);
// s == "Hello"


s.resize(8);
// s == "Hello???" ,?代表任何字符
1
2
3
4
5
6
(16)mid(int position, int n = -1)函数用于返回一个字符串,如果目标字符串包含从指定的位置索引开始的n个字符,则返回指定位置开始的由n个字符组成的字符串;如果位置索引超过了字符串的长度,则返回空字符串;如果从给定位置开始的字符串中有少于n个字符,或者如果n是-1(默认),函数将返回指定位置可用的所有字符。


QString x = "Nine pineapples";
QString y = x.mid(5, 4);            // y == "pine"
QString z = x.mid(5);               // z == "pineapples"
1
2
3
(17)prepend(const QString & str)函数将字符串str预先设置为该字符串的开头,并返回对该字符串的引用。


QString x = "ship";
QString y = "air";
x.prepend(y);
// x == "airship"
1
2
3
4
(18)remove(int position, int n)函数用于从字符串中移除n个字符,从给定的位置开始,并返回对字符串的引用。 
如果指定的位置索引在字符串中,但是position + n位于字符串的末尾,那么字符串将在指定位置被截断。


QString s = "Montreal";
s.remove(1, 4);
// s == "Meal"
1
2
3
(19)​replace(int position, int n, const QString & after)函数,用字符串替换从索引位置开始的n个字符,并返回对该字符串的引用。 
注意:如果指定的位置索引在字符串中,但是position + n超出了字符串范围,那么n将会被调整到在字符串的末尾停止。


QString x = "Say yes!";
QString y = "no";
x.replace(4, 3, y);
// x == "Say no!"
1
2
3
4
(20)setNum(int n, int base = 10)函数,将字符串设置为指定的打印值,并返回对字符串的引用。 
默认值为10,必须在2到36之间。对于10以外的值,n被视为无符号整数。


QString str;
str.setNum(1234);       // str == "1234"
1
2
(21)转换,相信这些很好理解


double  toDouble(bool * ok = 0) const
float   toFloat(bool * ok = 0) const
int toInt(bool * ok = 0, int base = 10) const
QByteArray  toLatin1() const
QByteArray  toLocal8Bit() const
long    toLong(bool * ok = 0, int base = 10) const
qlonglong   toLongLong(bool * ok = 0, int base = 10) const
QString toLower() const
NSString *  toNSString() const
short   toShort(bool * ok = 0, int base = 10) const
std::string toStdString() const
std::wstring    toStdWString() const
uint    toUInt(bool * ok = 0, int base = 10) const
ulong   toULong(bool * ok = 0, int base = 10) const
qulonglong  toULongLong(bool * ok = 0, int base = 10) const
ushort  toUShort(bool * ok = 0, int base = 10) const
QVector<uint>   toUcs4() const
QString toUpper() const
QByteArray  toUtf8() const
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
(22)QString与QDateTime的相互转换 
1、QDateTime转为QString


QString str;  
QDateTime time;  


time = QDateTime::currentDateTime();  


str = time.toString("yyyy-MM-dd hh:mm:ss");  
1
2
3
4
5
6
2、QString转为QDateTime


QString str;  
QDateTime time;  


str = "2018-04-02 13:35:00";  


time = QDateTime::fromString(str, "yyyy-MM-dd hh:mm:ss");  
1
2
3
4
5
6
版权声明:本文为博主原创文章,转载请注明出处。 https://blog.youkuaiyun.com/qq_40194498/article/details/79629632
文章标签: QString
个人分类: QT
所属专栏: Qt之路
相关热词: QString( QString QSTRING QString、 qstring
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值