C++ 检查字符串是否为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4077940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if string is empty
提问by ilija veselica
I got a project in C++ which I need to edit. This is a declaration of variable:
我有一个需要编辑的 C++ 项目。这是变量的声明:
LPSTR hwndTitleValue = (LPSTR)GlobalAlloc(GPTR,(sizeof(CHAR) * hwndTitleSize));
How to check if this string is empty?
如何检查这个字符串是否为空?
I tried simply with if(hwndTitleValue == "")
but it always returns false. How to check if this string is empty?
我简单地尝试过,if(hwndTitleValue == "")
但它总是返回 false。如何检查这个字符串是否为空?
EDIT
编辑
I also need to check if the file is attached. Here is the code of the file:
我还需要检查文件是否已附加。这是文件的代码:
// Attachment
OFSTRUCT ofstruct;
HFILE hFile = OpenFile( mmsHandle->hTemporalFileName , &ofstruct , OF_READ );
DWORD hFileSize = GetFileSize( (HANDLE) hFile , NULL );
LPSTR hFileBuffer = (LPSTR)GlobalAlloc(GPTR, sizeof(CHAR) * hFileSize );
DWORD hFileSizeReaded = 0;
ReadFile( (HANDLE) hFile , hFileBuffer, hFileSize, &hFileSizeReaded, NULL );
CloseHandle( (HANDLE) hFile );
How to check if hFile
is empty?
如何检查是否hFile
为空?
回答by Graeme Perrow
The easiest way to check if a string is empty is to see if the first character is a null byte:
检查字符串是否为空的最简单方法是查看第一个字符是否为空字节:
if( hwndTitleValue != NULL && hwndTitleValue[0] == '#include <string.h>
// ... other includes ...
int isEmpty(LPSTR string)
{
if (string != NULL)
{
// Use not on the result below because it returns 0 when the strings are equal,
// and we want TRUE (1).
return !strcmp(string, "");
}
return FALSE;
}
' ) {
// empty
}
You can use strlen
or strcmp
as in other answers, but this saves a function call.
您可以在其他答案中使用strlen
或strcmp
,但这可以节省函数调用。
回答by Bruno Brant
I believe that hwndTitleValue is a pointer, at least in Hungarian Notation it would be. Your method is allocating a array of bytes (an ANSI C string), so the best way to do it would be
我相信 hwndTitleValue 是一个指针,至少在匈牙利表示法中是这样。您的方法正在分配一个字节数组(ANSI C 字符串),因此最好的方法是
#include <string.h>
// ... other includes ...
int isEmpty(LPSTR string)
{
// Using the tip from Maciej Hehl
return (string != NULL && string[0] == 0);
}
You can, however, hack the above method and not use strcmp:
但是,您可以破解上述方法而不使用 strcmp:
#include <string.h>
// ... other includes ...
int isEmpty(LPSTR string)
{
// Always return FALSE to NULL pointers.
if (string == NULL) return FALSE;
// Use not on the result below because it returns 0 when the strings are equal,
// and we want TRUE (1).
return !strcmp(string, "");
}
One thing to note is that the string might not be empty but filled with space. This method will tell you that the string has data (spaces are data!). If you need to account for strings filled with spaces, you will need to trim it first.
需要注意的一件事是字符串可能不是空的,而是充满了空格。这个方法会告诉你字符串有数据(空格是数据!)。如果您需要考虑用空格填充的字符串,则需要先修剪它。
EDIT: Another thing to note is that the methods above do not account from NULL pointers correctly. If the pointer is null, isEmpty
will return FALSE, which isn't desired. We can remove the NULL check and then it becomes responsibility of the caller, or we can define that isEmpty returns FALSE to NULL strings.
编辑:另一件需要注意的事情是,上面的方法没有正确地从 NULL 指针开始计算。如果指针为空,isEmpty
将返回 FALSE,这是不希望的。我们可以删除 NULL 检查,然后它成为调用者的责任,或者我们可以定义 isEmpty 将 FALSE 返回到 NULL 字符串。
if (strlen(hwndTitleValve) == 0)
回答by Nikolai Fetissov
First of all, this is not a string. Not yet. It's just a pointer to a chunk of memory that for all intents and purposes contains garbage, i.e. some random data.
首先,这不是字符串。还没有。它只是一个指向所有意图和目的都包含垃圾的内存块的指针,即一些随机数据。
Strings in C are pointers to zero-terminatedcharacter arrays. So your empty string ""
is actually an array of one element with value zero. But your comparison is between pointers, so it always fails.
C 中的字符串是指向以零结尾的字符数组的指针。所以你的空字符串""
实际上是一个值为零的元素的数组。但是你的比较是在pointers之间进行的,所以它总是失败。
回答by uesp
The GlobalAlloc function just allocates and returns a block of memory and the GPTR option zeroes the bytes of the allocated memory so you can just use:
GlobalAlloc 函数只分配并返回一块内存,GPTR 选项将分配的内存的字节归零,因此您可以使用:
if (*hwndTitleValve == 0 ) {
}
assuming an ANSI string. Note that this would be better tagged as "C" and "Windows" rather than C++.
假设是 ANSI 字符串。请注意,最好将其标记为“C”和“Windows”而不是 C++。
回答by sharptooth
GlobalAlloc()
will return a memory block filled with zeroes (thanks to GPTR
flag), not a string. There's no point in checking. You'd better checked that the pointer returned is not null.
GlobalAlloc()
将返回一个填充零的内存块(感谢GPTR
标志),而不是一个字符串。检查没有意义。你最好检查一下返回的指针是否为空。
If that is not enough for you an just check
如果这对您来说还不够,请检查一下
##代码##A valid empty string will store a null terminator at the very beginning.
有效的空字符串将在开头存储一个空终止符。
回答by Nemanja Trifunovic
I find it strange that a string name starts with hwnd (that's for windows handles), but anyway you can assume that LPSTR is the same thing as char* and just use something like strlen to check its length.
我觉得奇怪的是,字符串名称以 hwnd 开头(用于 Windows 句柄),但无论如何您可以假设 LPSTR 与 char* 相同,只需使用 strlen 之类的东西来检查其长度。
回答by Kra
if you want to check whether memory allocation failed do it this way:
如果要检查内存分配是否失败,请这样做:
HGLOBAL hwndTitleValue = GlobalAlloc(GPTR,(sizeof(CHAR) * hwndTitleSize));
HGLOBAL hwndTitleValue = GlobalAlloc(GPTR,(sizeof(CHAR) * hwndTitleSize));
if (hwndTitleValue == NULL) return ALLOC_FAILED;
如果(hwndTitleValue == NULL)返回ALLOC_FAILED;
I see no point working with strings here.
我认为在这里处理字符串没有意义。