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.
The problem with this:
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:
or somewhat shorter: