判断C风格字符串是否为空
判断一个 const char* s 的字符串是否长度为 0 或不为 0 ,不需要 strlen(s)。
可以使用以下的方法:
const char* str = "AA";
if (!str || !*str)
cout << "str is empty" << endl;
else
cout << "str is not empty" << endl;
这里的两个判断条件:
- !str : 判断str 指针是否为空
- !*str : 判断字符串的第一个字符是否为
\0
,C风格字符串最后的是以\0
为结尾。