字符
字符分爲各種類別:數字,字母,空格和標點符號。QString
由QChars組成。QChar
用於isDigit()
,isLetter()
, isSpace()
和isPunct()
方法。
// letters.cpp
#include <QTextStream>
int main(void) {
QTextStream out(stdout);
int digits = 0;
int letters = 0;
int spaces = 0;
int puncts = 0;
QString str = "7 white, 3 red roses.";
foreach(QChar s, str) {
if (s.isDigit()) {
digits++;
} else if (s.isLetter()) {
letters++;
} else if (s.isSpace()) {
spaces++;
} else if (s.isPunct()) {
puncts++;
}
}
out << QString("There are %1 characters").arg(str.count()) << endl;
out << QString("There are %1 letters").arg(letters) << endl;
out << QString("There are %1 digits").arg(digits) << endl;
out << QString("There are %1 spaces").arg(spaces) << endl;
out << QString("There are %1 punctuation characters").arg(puncts) << endl;
return 0;
}
在這個例子中我們將統計字母、數字、空格、標點的累計數量。
foreach(QChar s, str) {
if (s.isDigit()) {
digits++;
} else if (s.isLetter()) {
letters++;
} else if (s.isSpace()) {
spaces++;
} else if (s.isPunct()) {
puncts++;
}
}
採用foreach
遍歷整個字符串,然後用isDigit()
判斷是否是數字,用isLetter()
判斷是否是字母,用isSpace()
判斷是否是空格,用isPunct()
判斷是否是標點。
輸出結果爲:
$ ./letters
There are 21 characters
There are 13 letters
There are 2 digits
There are 4 spaces
There are 2 punctuation characters
修改字符串
一些方法(例如tolower()
方法)會返回原始字符串的新修改副本。其他的一些方法就地修改字符串。
// modify.cpp
#include <QTextStream>
int main(void) {
QTextStream out(stdout);
QString str = "Lovely";
str.append(" season");
out << str << endl;
str.remove(10, 3);
out << str << endl;
str.replace(7, 3, "girl");
out << str << endl;
str.clear();
if (str.isEmpty()) {
out << "The string is empty" << endl;
}
return 0;
}
str.append(" season");
在字符串後面添加字符串。
str.remove(10, 3);
從字符串第十個字符開始移除三個字符。
str.replace(7, 3, "girl");
用特定字符串替換原有字符串的第七個字符開始的三個字符。
str.clear();
清空字符串內容。
輸出結果:
$ ./modify
Lovely season
Lovely sea
Lovely girl
The string is empty
字符串對齊
使用leftJustified()
和rightJustified()
方法來對齊字符串。
// right_align.cpp
#include <QTextStream>
int main(void) {
QTextStream out(stdout);
QString field1 = "Name: ";
QString field2 = "Occupation: ";
QString field3 = "Residence: ";
QString field4 = "Marital status: ";
int width = field4.size(); //計算最寬字符長度
out << field1.rightJustified(width, ' ') << "Robert\n";
out << field2.rightJustified(width, ' ') << "programmer\n";
out << field3.rightJustified(width, ' ') << "New York\n";
out << field4.rightJustified(width, ' ') << "single\n";
return 0;
}
該示例將字段字符串對齊到右側。
out << field1.rightJustified(width, ' ') << "Robert\n";
out << field2.rightJustified(width, ' ') << "programmer\n";
out << field3.rightJustified(width, ' ') << "New York\n";
out << field4.rightJustified(width, ' ') << "single\n";
rightJustified()
方法返回一個具有width
字符的字符串。如果字符串較短,其餘部分填充所提供的字符。在我們的例子中,它是一個空格字符。
輸出結果爲:
$ ./right_align
Name: Robert
Occupation: programmer
Residence: New York
Marital status: single
轉義字符
Qt5有一個toHtmlEscaped()
方法,它將純文本字符串轉換爲帶有HTML元字符<
,>
,&
和"
的HTML字符串,並由HTML命名實體替換。
$ cat cprog.c
#include <stdio.h>
int main(void) {
for (int i=1; i<=10; i++) {
printf("Bottle %d\n", i);
}
}
這個c程序包含HTML元字符。
// html_escape.cpp
#include <QTextStream>
#include <QFile>
int main(void) {
QTextStream out(stdout);
QFile file("cprog.c");
if (!file.open(QIODevice::ReadOnly)) {
qWarning("Cannot open file for reading");
return 1;
}
QTextStream in(&file);
QString allText = in.readAll();
out << allText.toHtmlEscaped() << endl;
file.close();
return 0;
該示例讀取一個c程序並用它們的命名實體替換元字符。
如果直接運行qmake -project
,生成的.pro
文件內容如下:
######################################################################
# Automatically generated by qmake (3.1) Mon Feb 12 19:47:55 2018
######################################################################
TEMPLATE = app
TARGET = html_escape
INCLUDEPATH += .
# The following define makes your compiler warn you if you use any
# feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000
# disables all the APIs deprecated before Qt 6.0.0
# Input
SOURCES += cprog.c html_escape.cpp
需要在最後一行加上QT -= gui
,然後執行qmake
、make
命令會報如下錯誤:
...
html_escape.o:在函數‘main’中:
html_escape.cpp:(.text.startup+0x0): main 的多重定義
cprog.o:cprog.c:(.text.startup+0x0):第一次在此定義
collect2: error: ld returned 1 exit status
Makefile:248: recipe for target 'html_escape' failed
make: *** [html_escape] Error
解決方法爲將.pro
文件中SOURCES += cprog.c html_escape.cpp
改爲SOURCES += html_escape.cpp
,再執行make
即可。
輸出結果爲:
$ ./html_escape
#include <stdio.h>
int main(void) {
for (int i=1; i<=10; i++) {
printf("Bottle %d\n", i);
}
}