String.IsNullOrEmpty()方法以及C#中的示例

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

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值