text 检查C中的空字符串

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了text 检查C中的空字符串相关的知识,希望对你有一定的参考价值。

To check if a string is empty is useful like when user is prompt to input a string, but maybe the user only hit "enter" and didn't type anything, now we get an empty string.

So usually if we get user to input a string, we should check first if it's empty or not. Only if it's not empty then we do things with the string.

Since C-style strings are always terminated with the null character (\0), you can check whether the string is empty in 3 different ways:

1. Check if the first char in the string is '\0':

do {
   ...
} while (url[0] == '\0');

2. If the string is empty, the string length is 0, but there will be 1 byte in the memory because there is a '\0', so we just need to check if the string length is bigger than 0:

if ((strlen(t)>0) //only if it's not an empty string then will go into the brackets of "if"
{
  ...
}

3. Alternatively, you could use the strcmp function, which is overkill but might be easier to read:

do {
   ...
} while (strcmp(url, ""));

Note that strcmp returns a nonzero value if the strings are different and 0 if they're the same, so this loop continues to loop until the string is empty.

以上是关于text 检查C中的空字符串的主要内容,如果未能解决你的问题,请参考以下文章

如何检查@RequestMapping 参数中的请求参数是不是为空字符串?

为啥'if not'检查python中的空字符串[重复]

Android检查Android中的null或空字符串

spark中的isNullOrEmpty函数检查数据框中的列是不是为空或空字符串

检查一个键是不是存在并且它的值不是 Python 字典中的空字符串

javascript检查空字符串或空字符串[重复]