Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if string is empty

Tags:

c++

string

I got a project in C++ which I need to edit. This is a declaration of variable:

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?

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?

like image 870
ilija veselica Avatar asked Nov 02 '10 13:11

ilija veselica


People also ask

Is string null or empty?

The Java programming language distinguishes between null and empty strings. An empty string is a string instance of zero length, whereas a null string has no value at all. An empty string is represented as "" . It is a character sequence of zero characters.

Does isEmpty work on string?

isEmpty() String method checks whether a String is empty or not. This method returns true if the given string is empty, else it returns false. The isEmpty() method of String class is included in java string since JDK 1.6. In other words, you can say that this method returns true if the length of the string is 0.

Is Blank vs isEmpty?

isBlank() vs isEmpty() The difference between both methods is that isEmpty() method returns true if, and only if, string length is 0. isBlank() method only checks for non-whitespace characters. It does not check the string length.

How do I check if a string is empty or null in Java 8?

Using the isEmpty() Method The isEmpty() method returns true or false depending on whether or not our string contains any text. It's easily chainable with a string == null check, and can even differentiate between blank and empty strings: String string = "Hello there"; if (string == null || string. isEmpty() || string.


1 Answers

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] == '\0' ) {
    // empty
}

You can use strlen or strcmp as in other answers, but this saves a function call.

like image 51
Graeme Perrow Avatar answered Sep 29 '22 11:09

Graeme Perrow