String.IsNullOrWhiteSpace(String) Metode

Definisi

Menunjukkan apakah string yang ditentukan adalah null, kosong, atau hanya terdiri dari karakter spasi putih.

public:
 static bool IsNullOrWhiteSpace(System::String ^ value);
public static bool IsNullOrWhiteSpace (string value);
public static bool IsNullOrWhiteSpace (string? value);
static member IsNullOrWhiteSpace : string -> bool
Public Shared Function IsNullOrWhiteSpace (value As String) As Boolean

Parameter

value
String

String yang akan diuji.

Mengembalikan

truevalue jika parameter adalah null atau Empty, atau jika value terdiri dari karakter spasi kosong secara eksklusif.

Contoh

Contoh berikut membuat array string, lalu meneruskan setiap elemen array ke IsNullOrWhiteSpace metode .

using System;

public class Example
{
   public static void Main()
   {
      string[] values = { null, String.Empty, "ABCDE", 
                          new String(' ', 20), "  \t   ", 
                          new String('\u2000', 10) };
      foreach (string value in values)
         Console.WriteLine(String.IsNullOrWhiteSpace(value));
   }
}
// The example displays the following output:
//       True
//       True
//       False
//       True
//       True
//       True
open System

let values = 
    [| null; String.Empty; "ABCDE"
       String(' ', 20); "  \t   "
       String('\u2000', 10) |]

for value in values do
    printfn $"{String.IsNullOrWhiteSpace value}"
// The example displays the following output:
//       True
//       True
//       False
//       True
//       True
//       True
Module Example
   Public Sub Main()
      Dim values() As String = { Nothing, String.Empty, "ABCDE", 
                                 New String(" "c, 20), "  " + vbTab + "   ", 
                                 New String(ChrW(&h2000), 10) }
      For Each value As String In values
         Console.WriteLine(String.IsNullOrWhiteSpace(value))
      Next
   End Sub
End Module
' The example displays the following output:
'       True
'       True
'       False
'       True
'       True
'       True

Keterangan

IsNullOrWhiteSpace adalah metode kenyamanan yang mirip dengan kode berikut, kecuali bahwa ia menawarkan performa yang unggul:

return String.IsNullOrEmpty(value) || value.Trim().Length == 0;
String.IsNullOrEmpty value || value.Trim().Length = 0
Return String.IsNullOrEmpty(value) OrElse value.Trim().Length = 0

Karakter spasi putih ditentukan oleh standar Unicode. Metode ini IsNullOrWhiteSpace menafsirkan karakter apa pun yang mengembalikan nilai true ketika diteruskan ke Char.IsWhiteSpace metode sebagai karakter spasi putih.

Berlaku untuk

Lihat juga