Testing if a string is empty
-
Hi all, Simple question .... (funny that I can't remember how to do it) I have variable:
char TestString[3];
How can one test if the TestString is Empty :confused: I have tried the following, without success:if (TestString == "") { .... }
Thanx in advanceThe only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r
-
Hi all, Simple question .... (funny that I can't remember how to do it) I have variable:
char TestString[3];
How can one test if the TestString is Empty :confused: I have tried the following, without success:if (TestString == "") { .... }
Thanx in advanceThe only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r
strlen
.Programm3r wrote:
char TestString[3];
In this case, it will not be an empty string.Contains some garbage. try,
char TestString[3] = {0};
or
char TestString[3] = "";
cout<It will give you desired output. i.e. output will be 0, which indiacates empty string.
Prasad
Notifier using ATL | Operator new[],delete[][^] -
Hi all, Simple question .... (funny that I can't remember how to do it) I have variable:
char TestString[3];
How can one test if the TestString is Empty :confused: I have tried the following, without success:if (TestString == "") { .... }
Thanx in advanceThe only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r
You need to do if(strlen(TestString) == 0) or if(strcmp(TestString, "") == 0) Remember that your string will not be initialised when you declare it though, so neither test is valid until you've put something into the string (NULL for the first character at the very least). Or you can do the following... memset(&TestString[0], 0, sizeof(TestString)); Hope this helps, Joel.
-
Hi all, Simple question .... (funny that I can't remember how to do it) I have variable:
char TestString[3];
How can one test if the TestString is Empty :confused: I have tried the following, without success:if (TestString == "") { .... }
Thanx in advanceThe only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r
Your string
Programm3r wrote:
char TestString[3];
``is not empty, it's random (junk) initialised (end it is not `NULL` terminated!). maybe the following code snipped will help you: char TestString[3]=""; if (!strcmp(TestString, "")) { printf("empty\n"); } else { printf("not empty: %s\n", TestString); } If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile. ``
-
Hi all, Simple question .... (funny that I can't remember how to do it) I have variable:
char TestString[3];
How can one test if the TestString is Empty :confused: I have tried the following, without success:if (TestString == "") { .... }
Thanx in advanceThe only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r
if (!TestString[0]) { // The first character is null, therefore it's empty } That's assuming you have set it to be empty. This is one of many reasons to avoid char arrays and use std::string or CString. Unless you're trapped in C, which I seem to recall, you are. In this case, make sure you use memset to clear the contents of a new char array, so the above test will work. All the other tests will do the same thing, they will look to see how many characters there are before a NULL.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
Hi all, Simple question .... (funny that I can't remember how to do it) I have variable:
char TestString[3];
How can one test if the TestString is Empty :confused: I have tried the following, without success:if (TestString == "") { .... }
Thanx in advanceThe only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r
:):-D:laugh: Regards
The only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r
-
:):-D:laugh: Regards
The only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r
In addition to all the things said, it's always good programming practice to initialize the variables before testing on them. IMO, one common reason for problem is that memory is allocated differently in debug and release mode. In debug, the allocated buffer becomes filled with null-bytes. In release-build, it isn't. It's filled with junk characters (whatever is on the stack). So (without initializing) testing for '\0' at index 0 works in debug mode, but not in release mode. That's something to watch out for..
Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson
-
In addition to all the things said, it's always good programming practice to initialize the variables before testing on them. IMO, one common reason for problem is that memory is allocated differently in debug and release mode. In debug, the allocated buffer becomes filled with null-bytes. In release-build, it isn't. It's filled with junk characters (whatever is on the stack). So (without initializing) testing for '\0' at index 0 works in debug mode, but not in release mode. That's something to watch out for..
Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson
kakan wrote:
n debug, the allocated buffer becomes filled with null-bytes. In release-build, it isn't. It's filled with junk characters (whatever is on the stack).
and this is the source of very weird bugs. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.