1

I want to check if a string object of 20 characters has only null characters in it (values of zero). My attempt:

string subString;
subString = fileBuffer.substr(origin, origin+20);

if(strCompare.compare("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0") == 0)
    cout<<"string is empty"<<endl;
else
    cout<<"string is not empty"<<endl;

I am certain that subString is assigned 20 characters of null, however, the program only outputs "string is not empty". I tried other methods too such as making the compare parameter to "" or NULL to no avail. Can someone point out to me of any obvious errors or of the correct way to do this? I would really appreciate it.

1
  • 1
    Well, unlike a C string, a std::string containing 20 null characters is not empty, so the output ain't lying :p Commented Jan 29, 2010 at 22:29

5 Answers 5

6

The problem with this:

 if(strCompare.compare("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0")

is that the string constructed from the C-style string will be empty. You need to construct a string explicitly, providing the size. One way:

 if( strCompare.compare(
     std::string( "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 20 ) == 0 )

or somewhat shorter:

 if( strCompare.compare(
     std::string( 20, 0 ) == 0 )
Sign up to request clarification or add additional context in comments.

3 Comments

Would the string have any nulls in it? I think it'd be empty.
It would have 20 nulls - std::string can happily contain null (or any other) characters.
I was referring to "the string constructed from the C-style string contains only a single NULL - the first one." I believe it would be a string of size 0. I think the constructor you use will work.
3

Try find_first_not_of:

if (subString.find_first_not_of('\0') == string::npos)
{
    cout << "Empty" << endl;
}
else
{
    cout << "Not empty" << endl;
}

4 Comments

+1 But I think you might want to swap the output or change != into ==
Also, std::string has plenty of overloads: if (subString.find_first_not_of('\0') == std::string::npos) is even more convenient.
You're right again, find_first_not_of() does have that overload. I'll change it.
Happy now. (I'd delete my comments if you deleted yours.)
2

First of all, when you write "string" it is not really a string in the C/C++ sense. A string is a set of characters with an ending '\0'. Normal string compare operations don't help here as they stop when they see the first '\0'. Thus, you should say that you want to test if the "buffer" is all empty. Also, std::string may optimize the string you're inserting in if it has only null characters, and store a string of zero size. However, you can use something like memcmp for that purpose:

 char buffer[20];
 infile.read(buffer, 20);
 if (!memcmp(buffer, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 20)
 {
     // it is empty
 }

Other approaches exist, but take into account the definition of C strings.

2 Comments

He is asking about C++ strings, not C-style strings.
Neil: yes, you're right. But it seemed to me that he or she had to have better knowledge of null terminated strings. You're right about the std::string with the "20" size parameter, but it is confusing, at the least, that the example works with that number, and not without it.
2

Your question is formulated rather weirdly. "I want to check if a string object of 20 characters has only null characters in it (values of zero)." What about strings of 30 characters? 5 characters? Do you need to check them as well? If you do, then what about a string of 30 zeroes, should it output "string is empty" or not? The intent expressed by your code appears to suggest that a string of 30 zeroes shouldn't be considered "empty". But that's kind of weird. I'd expect all string of zeroes to be considered "empty" (even though an all-zero string is not really empty), regardless of length.

Anyway, if you want to detect all strings consisting of zeroes only, then the right thing to use would be the find_first_not_of method (already suggested)

if (subString.find_first_not_of('\0') == subString::npos)
  cout << "string is empty" << endl; 
else 
  cout << "string is not empty" << endl; 

Although if your intent is to detect strings that are empty when interpreted as C-strings, the only thing you need to check is the first character.

Comments

1

Try memcmp

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.