如何在 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