เมธอด IsNullOrWhiteSpace() ใน C# ใช้เพื่อระบุว่าสตริงที่ระบุเป็นค่าว่าง ว่าง หรือประกอบด้วยอักขระช่องว่างเท่านั้น
ไวยากรณ์
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 = "\n";
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