C# IsNullOrWhiteSpace() 方法
C# 中的 IsNullOrWhiteSpace() 方法用於指示指定的字串是否為 null、空或僅由空格符組成。
語法
public static bool IsNullOrWhiteSpace (string val);
上方,val 引數是要測試的字串。現在我們來看一個示例 −
示例
現在我們來看一個示例
using System;
public class Demo {
public static void Main() {
string str1 = null;
string str2 = String.Empty;
Console.WriteLine("Is string1 null or whitespace? = "+String.IsNullOrWhiteSpace(str1));
Console.WriteLine("Is string2 null or whitespace? = "+String.IsNullOrWhiteSpace(str2));
}
}輸出
這將產生以下輸出 -
Is string1 null or whitespace? = True Is string2 null or whitespace? = True
示例
現在我們來看另一個示例 -
using System;
public class Demo {
public static void Main() {
string str1 = "
";
string str2 = "Tim";
Console.WriteLine("Is string1 null or whitespace? = "+String.IsNullOrWhiteSpace(str1));
Console.WriteLine("Is string2 null or whitespace? = "+String.IsNullOrWhiteSpace(str2));
}
}輸出
Is string1 null or whitespace? = True Is string2 null or whitespace? = False
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP