如何在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 教师闺蜜祝福语简短