Check if string is empty or all spaces in C#

前端 未结 5 854
说谎
说谎 2020-12-09 14:32

How to easily check if a string is blank or full of an undetermined amount of spaces, or not?

5条回答
  •  忘掉有多难
    2020-12-09 15:23

    private bool IsNullOrEmptyOrAllSpaces(string str)
    {
        if(str == null || str.Length == 0)
        {
            return true;
        }
    
        for (int i = 0; i < str.Length; i++)
        {
            if (!Char.IsWhiteSpace(str[i])) return false;
        }
    
        return true;
    }
    

提交回复
热议问题