如何在 C# 中使用 string.Empty 或 String.Empty 来初始化字符串?
在 C# 中使用 string.Empty 将字符串设置为空 −
string myStr = string.Empty;
要检查它是否为字符串,请使用 IsNullOrEmpty() 方法 −
if (string.IsNullOrEmpty(myStr)) {
Console.WriteLine("String is empty or null!");
}以下是一个示例 −
示例
using System;
namespace Demo {
public class Program {
public static void Main(string[] args) {
string myStr = string.Empty;
if (string.IsNullOrEmpty(myStr)) {
Console.WriteLine("String is empty or null!");
} else {
Console.WriteLine("String isn't empty or null!");
}
}
}
}输出
String is empty or null!
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP