如何在C#中将字符串初始化为空字符串?
要将字符串初始化为空列表-
string myStr = null;
现在,使用内置方法IsNullOrEmpty()检查列表是否为空-
if (string.IsNullOrEmpty(myStr)) {
Console.WriteLine("字符串为空或空!");
}让我们看完整的代码-
示例
using System;
namespace Demo {
class Program {
static void Main(string[] args) {
string myStr = null;
if (string.IsNullOrEmpty(myStr)) {
Console.WriteLine("字符串为空或空!");
}
Console.ReadKey();
}
}
}输出结果
字符串为空或空!
将字符串初始化为空字符串的另一种方法,请尝试以下代码。在这里,我们使用了string.Empty-
示例
using System;
namespace Demo {
public class Program {
public static void Main(string[] args) {
string myStr = string.Empty;
if (string.IsNullOrEmpty(myStr)) {
Console.WriteLine("字符串为空或空!");
} else {
Console.WriteLine("String isn't empty or null!");
}
}
}
}输出结果
字符串为空或空!
热门推荐
8 十月简短祝福语
10 宝宝生日祝福语大全简短
11 50多岁生日简短祝福语
12 天气转凉祝福语简短
13 空军大学开学祝福语简短
14 外婆祝福语短句唯美简短
15 祝福语赠言给老师简短
16 长辈钻石婚祝福语简短
17 朋友家住豪宅祝福语简短
18 男童毕业蛋糕祝福语简短