C#String.IsNullOrEmpty()方法 (C# String.IsNullOrEmpty() Method)
String.IsNullOrEmpty() method is a built-in method of String class and it is used to check whether a string is Null or Empty? If string object is not initialized with a correct value it will be considered as "null string", if string object is initialized but contains nothing i.e. it is assigned the value ("") it will be considered as "empty string".
String.IsNullOrEmpty()方法是String类的内置方法,用于检查字符串是Null还是Empty ? 如果未使用正确的值初始化字符串对象,则将其视为“空字符串” ;如果已初始化字符串对象但不包含任何值,即为该字符串对象分配了值( “” ),则将其视为“空字符串” 。
Syntax:
句法:
public static bool IsNullOrEmpty(String str);
The method is called with "string"/ "String". Here, "string" is an alias of "String" class.
用“字符串” /“字符串”调用该方法。 此处,“字符串”是“字符串”类的别名。
Parameter(s):
参数:
str – represents a string value or string object to be checked.
str –表示要检查的字符串值或字符串对象。
Return value:
返回值:
bool – it returns "True" if str is null or empty, otherwise it returns "False".
bool-如果str为null或为空,则返回“ True”,否则返回“ False”。
Example:
例:
Input:
string str1 = "";
string str2 = null;
string str3 = "IncludeHelp";
Function call
Console.WriteLine(string.IsNullOrEmpty(str1));
Console.WriteLine(string.IsNullOrEmpty(str2));
Console.WriteLine(string.IsNullOrEmpty(str3));
Output:
True
True
False
C#使用String.IsNullOrEmpty()方法将字符串转换为字符数组的示例 (C# Example to convert string to characters array using String.IsNullOrEmpty() method)
Example 1:
范例1:
using System;
class IncludeHelp
{
static void Main()
{
// declaring string variables
string str1 = "";
string str2 = null;
string str3 = "IncludeHelp";
// check whether string is empty/null or not
Console.WriteLine(string.IsNullOrEmpty(str1));
Console.WriteLine(string.IsNullOrEmpty(str2));
Console.WriteLine(string.IsNullOrEmpty(str3));
}
}
Output
输出量
True
True
False
Example 2:
范例2:
using System;
class IncludeHelp
{
static void Main()
{
// declaring string variable
string str = "IncludeHelp";
// checking whether string is null/empty or not
if(string.IsNullOrEmpty(str))
Console.WriteLine("str is empty or null");
else
Console.WriteLine("str is not empty or null");
//now assigning null to the string
str = null;
// checking whether string is null/empty or not
if(string.IsNullOrEmpty(str))
Console.WriteLine("str is empty or null");
else
Console.WriteLine("str is not empty or null");
}
}
Output
输出量
str is not empty or null
str is empty or null
翻译自: https://www.includehelp.com/dot-net/string-isnullorempty-method-with-example-in-csharp.aspx