XE6 How do you check if a UnicodeString is null?

2019-07-03 19:20发布

问题:

I'm using C++ Builder XE6. I'm getting a UnicodeString as a parameter and I wish to check if the string is set to NULL, and not an empty string.

I've tried to do some simple compares to see if the param is null, but seem to be failing. I'm using the == operator which doesn't seem to be working, which makes me think it is overloaded.

I've tried:

if (searchString == NULL)

In the debugger view, it shows the value of { NULL } in the local variables. If I add the variable to the watch list, then it shows it has a property of "Data" which is NULL.

Any ideas on how I can properly do the compare?

回答1:

There is no such thing as a NULL value for a UnicodeString. A string is a series of characters, but NULL is a pointer (well - actually it is a macro that evaluates to an int, 0, but it is supposed to be used to indicate null pointers if your compiler doesn't support nullptr).

Internally, the data member of UnicodeString is NULL when the string is empty, and non-NULL when the string has at least 1 character.

To check if the string is empty, use the IsEmpty() method, which checks if the data member is NULL or not. There is only one empty state; there is no distinction between "empty" and "null" like some languages have.

The value you see in the debugger is the internal data member of UnicodeString, it is not a part of UnicodeString's interface. When you see NULL in the debugger, you should treat it as being an empty string.