如何将QString初始化为null? [英] how to initialize a QString to null?
问题描述
QString :: number(0)
和((const char *)0)
有什么区别?
我想将 phoneNumber
的 QString
初始化为 null
. phoneNumber(QString :: number(0))
和 phoneNumber((const char *)0)
都可以吗?
要创建一个空的 QString
,只需默认对其进行初始化:
QString phoneNumber;//,或者如果您已经有一个QString变量并想清除"它:phoneNumber = QString();
请注意, QString :: number(0)
绝对为 not null-它会创建一个值为"0"
的QString./p>
您还可以使用 NULL
指针初始化 QString
,但是除非您传递一个指针,无论它是否为 NULL,否则我不建议这样做
(不是)(即有时可能指向C字符串),因为它是不必要的.
您还应该了解遵循Qt文档:
空字符串与空字符串的区别
出于历史原因,
QString
区分空字符串和一个空字符串.空字符串是已初始化的字符串使用QString
的默认构造函数或通过传递(const char *)0
到构造函数.空字符串是任何大小为0的字符串.null字符串始终为空,但空字符串不一定为null:QString().isNull();//返回trueQString().isEmpty();//返回trueQString(").isNull();//返回falseQString(").isEmpty();//返回trueQString("abc").isNull();//返回falseQString("abc").isEmpty();//返回false
除
isNull()
之外的所有函数都将空字符串视为空字符串.例如,toAscii().constData()
返回一个指向'\ 0'
字符表示空字符串(不是空指针),并且QString()
比较等于QString(")
.我们建议您始终使用isEmpty()
函数,并避免isNull()
.
What is the difference between QString::number(0)
and ((const char*) 0)
?
I want to initialize a QString
say phoneNumber
to null
. Will phoneNumber(QString::number(0))
and phoneNumber((const char*) 0)
both work?
To create a null QString
just default initialize it:
QString phoneNumber;
// or if you already have a QString variable and want to 'clear' it:
phoneNumber = QString();
Note that QString::number(0)
is decidedly not null - it creates a QString with the value "0"
.
You could also initialize the QString
with a NULL
pointer, but I wouldn't recommend it unless you're passing a pointer regardless of whether it's NULL
or not (i.e., it could sometimes point to a C string) since it's unnecessary.
You should also understand the following Qt docs:
Distinction Between Null and Empty Strings
For historical reasons,
QString
distinguishes between a null string and an empty string. A null string is a string that is initialized usingQString
's default constructor or by passing(const char *)0
to the constructor. An empty string is any string with size 0. A null string is always empty, but an empty string isn't necessarily null:QString().isNull(); // returns true QString().isEmpty(); // returns true QString("").isNull(); // returns false QString("").isEmpty(); // returns true QString("abc").isNull(); // returns false QString("abc").isEmpty(); // returns false
All functions except
isNull()
treat null strings the same as empty strings. For example,toAscii().constData()
returns a pointer to a'\0'
character for a null string (not a null pointer), andQString()
compares equal toQString("")
. We recommend that you always use theisEmpty()
function and avoidisNull()
.
这篇关于如何将QString初始化为null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!