QString - 查找字符串数据
QString::startsWith
void startsWith_test() // 判断是否以某个字符串开头
{
QString str = "Welcome to you! ";
// 第一个参数 :指定字符串
// 第二个参数 :指定大小写是否敏感
str.startsWith("Welcome", Qt::CaseInsensitive); // 返回 true
str.startsWith("you", Qt::CaseInsensitive); // 返回 false
}
QString::endsWith
void endsWith_test()
{
QString str = "Welcome to you!";
// 第一个参数 :指定字符串
// 第二个参数 :指定大小写是否敏感
qDebug() << str.endsWith("Welcome", Qt::CaseInsensitive); // 返回 false
qDebug() << str.endsWith("you!", Qt::CaseInsensitive); // 返回 true
}
QString::compare
compare(const QString &, const QString &, Qt::CaseSensitivity);
该函数可以指定是否进行大小写比较,而大小写比较是完全基于Unicode编码值的。
------------------------------------------------------------------------------------------------
字符串的转化
void change_int()
{
QString str = "125";
bool ok; // 用于设置是否转换成功
// 第二个参数,指定转换的基数
/**
* 0x - 基数16
* 0 - 基数8
* 其他情况基数是 10
*/
int hex = str.toInt(&ok, 16); // ok = true,hex = 293
int dec = str.toInt(&ok, 10); // ok = true,dec = 125
qDebug() << hex;
qDebug() << dec;
}
void exchange_test()
{
QString str = " welcome to you! "; // 初始化一个字符串对象
QByteArray ba = str.toUtf8();
qDebug() << ba;
ba.append("Hello world! ");
qDebug() << ba.data();
}
QString提供的字符编码集的转换函数将会返回一个const char*类型版本的 QByteArray 即构造函数QByteArray(const char*)构造的QByteArray对象。
QByteArray类具有一个字节它既可以存储原始字节 (raw bytes),也可以存储传统的以“0”结尾的 8 位的字符串。在Qt中,使用QByteArray 比使用const char*更方便,且QByteArray 也支持隐式共享。
NULL字符串和空(empty)字符串的区别
void Null_or_Empty()
{
QString().isNul1(); // true
Ostring().isEmpty(); // true
OString("").isNull(); // false
QString("").isEmpty(); // true
}