如何在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!");
}
}
}
}输出结果
字符串为空或空!
热门推荐
10 朋友新年祝福语大全 简短
11 新年祝福语简短大方兔年
12 搬新家礼物祝福语简短
13 同学见面花束祝福语简短
14 五一假期祝福语幽默简短
15 离职欢送敬酒祝福语简短
16 对学弟的祝福语简短
17 考老师辞职祝福语简短
18 祝福语驱散霉运的话简短