目錄
- 1. isNull()
- 2. isEmpty()
- 3. length()
- 4. truncate()
- 5. indexOf()/lastIndexOf()
- 6. arg()
- 7. at()
- 8. append()/prepend()
- 9.contains()
- 10. begin()/end()
- 11. insert()
- 12. count()
- 13. compare()
- 14.push_back()/push_front()
- 15. remove()
- 16. chop()
- 17. clear()
- 18. startsWith()/endsWith()
- 19. setNum()
- 20. replace()
- 21. toInt()/toLong()/toFloat()/toDouble...
1. isNull()
返回bool類型,如果爲空(QString str;str.isNull())就是true,否則false
2. isEmpty()
返回bool類型,如果爲空(QString str="";str.isNull())就是true,否則
false
3. length()
求長度
4. truncate()
截取字符串,並將截取部分賦給該字符串
QString str="Today is very good";
str.truncate(5);//str="Today"
5. indexOf()/lastIndexOf()
indexOf()::查找字符串,如果能夠找到則返回位置,找不到返回-1
接受三個參數,第二個參數默認0,從0向後遍歷,返回第一次出現的位置
int QString::indexOf(const QString & str, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
lastIndexOf():第二個參數默認值爲-1,從後向前遍歷,返回找到的第一個字符位置
int QString::lastIndexOf(const QString &str, int from = -1, Qt::CaseSensitivity cs =
Qt::CaseSensitive) const
6. arg()
返回類型是QString,可以接受三個參數,也可以接受兩個參數
fieldwidth:如果是正值則右對齊,負值則左對齊
base:代表進制
QString arg ( long a, int fieldwidth = 0, int base = 10 ) const
QString arg ( ulong a, int fieldwidth = 0, int base = 10 ) const
QString arg ( int a, int fieldwidth = 0, int base = 10 ) const
QString arg ( uint a, int fieldwidth = 0, int base = 10 ) const
QString arg ( short a, int fieldwidth = 0, int base = 10 ) const
QString arg ( ushort a, int fieldwidth = 0, int base = 10 ) const
QString arg ( char a, int fieldwidth = 0 ) const
QString arg ( QChar a, int fieldwidth = 0 ) const
QString arg ( const QString & a, int fieldwidth = 0 ) const
QString arg ( double a, int fieldwidth = 0, char fmt = 'g', int prec = -1 ) const
QString & sprintf ( const char * cformat, ... )
7. at()
返回類型是QChar,參數是從0開始的
QString str="wefrf";
str.at(1);//e
8. append()/prepend()
append:將要拼接的內容放在改字符串的後面
QString str1="asd";
QString str2="fgh";
str2.append(str1);//str2="asdfgh"
prepend:將要拼接的內容放在改字符串的前面
QString str1="asd";
QString str2="fgh";
str2.prepend(str1);//str2="fghasd"
`
9.contains()
返回bool類型,是否包含字符,字符串
QString str1="ww1eee";
if(str1.contains('e'))
qDebug()<<str1;
else
qDebug()<<"Nothing";
10. begin()/end()
begin:指向第一個字符
end:指向超尾
11. insert()
插入,第一個參數是插入的位置,第二個是插入的內容
QString str = "Meal";
str.insert(1, QString("ontr"));
// str == "Montreal"
12. count()
A在B中出現的次數
第二個參數是默認CaseSensitive(區分大小寫)
int count(const QString &str,
Qt::CaseSensitivity cs = Qt::CaseSensitive) const
QString str="wwmeelkjeoe";
int num=str.count('e');//4
13. compare()
比較s1和s2,如果s1小於、等於或大於s2,則返回一個小於、等於或大於0的整數。如果cs爲Qt::區分大小寫,則比較區分大小寫;否則,比較不區分大小寫(CaseInSensitive)。
int x = QString::compare("aUtO", "AuTo", Qt::CaseInsensitive); // x == 0
int y = QString::compare("auto", "Car", Qt::CaseSensitive); // y > 0
int z = QString::compare("auto", "Car", Qt::CaseInsensitive); // z < 0
14.push_back()/push_front()
push_back:同append()
push_back:同push_front()
15. remove()
1.QString &QString::remove(int position, int n)
從給定位置索引處開始(該位置不刪除),從字符串中刪除n個字符,並返回對該字符串的引用。如果指定的位置索引在字符串中,但是position + n超出了字符串的末尾,則在指定的位置截斷字符串。
2.QString &QString::remove(QChar ch, Qt::CaseSensitivity cs =Qt::CaseSensitive)
刪除該字符串中出現的每個ch,並返回對該字符串的引用。
如果cs是Qt::區分大小寫(默認),則搜索區分大小寫;否則,搜索不區分大小寫。
3.QString &QString::remove(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive)
刪除字符串中所有的str
16. chop()
void QString::chop(int n)
從末尾刪除n個
17. clear()
清空
18. startsWith()/endsWith()
返回bool類型,是否以。。。開始/結尾
19. setNum()
接受兩個參數,第一個參數是要轉換的數字,第二個參數是進制
20. replace()
返回QString,接受三個參數,第一個參數(被包括,從0開始),是開始位置,第二個參數是要代替字符的長度,第三個是要替換的內容
QString str="Today is very good";
str.replace(14,4,"bad");
21. toInt()/toLong()/toFloat()/toDouble…
Qstring類型轉換成數字