String.IsNullOrWhiteSpace(String) 方法
定義
表示指定的字串是否為 null
、空白,或只由空白字元組成的字串。Indicates whether a specified string is null
, empty, or consists only of white-space characters.
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
參數
- value
- String
要測試的字串。The string to test.
傳回
如果 true
參數是 value
或 null
,或者 Empty 完全由空白字元組成,則為 value
。true
if the value
parameter is null
or Empty, or if value
consists exclusively of white-space characters.
範例
下列範例會建立字串陣列,然後將陣列的每個元素傳遞給 IsNullOrWhiteSpace 方法。The following example creates a string array, and then passes each element of the array to the IsNullOrWhiteSpace method.
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
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
備註
IsNullOrWhiteSpace 是很方便的方法,類似于下列程式碼,不同之處在于它會提供較佳的效能:IsNullOrWhiteSpace is a convenience method that is similar to the following code, except that it offers superior performance:
return String.IsNullOrEmpty(value) || value.Trim().Length == 0;
Return String.IsNullOrEmpty(value) OrElse value.Trim().Length = 0
空白字元是由 Unicode 標準所定義。White-space characters are defined by the Unicode standard. IsNullOrWhiteSpace方法 true
會在傳遞至 Char.IsWhiteSpace 方法做為空白字元時,解讀傳回值的任何字元。The IsNullOrWhiteSpace method interprets any character that returns a value of true
when it is passed to the Char.IsWhiteSpace method as a white-space character.