Reputation: 2267
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.
Upvotes: 1
Views: 1523
Reputation: 320361
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.
Upvotes: 2
Reputation: 62053
Try find_first_not_of:
if (subString.find_first_not_of('\0') == string::npos)
{
cout << "Empty" << endl;
}
else
{
cout << "Not empty" << endl;
}
Upvotes: 3
Reputation:
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 )
Upvotes: 6
Reputation: 29001
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.
Upvotes: 2