C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
And: If the String reference is Nothing or the Length is zero it returns True. Otherwise it returns False.
VB.NET program that uses String.IsNullOrEmpty
Module Module1
Sub Main()
' Test a Nothing String.
Dim test1 As String = Nothing
Dim result1 As Boolean = String.IsNullOrEmpty(test1)
Console.WriteLine(result1)
' Test an empty String.
Dim test2 As String = ""
If (String.IsNullOrEmpty(test2)) Then
Console.WriteLine("Is empty")
End If
End Sub
End Module
Output
True
Is empty
So: In other words, String.IsNullOrWhiteSpace will return True if any of these conditions matches.
1. Nothing: The String reference itself equals Nothing. This is another term for null.
2. Empty: The String data is empty. IsNullOrWhiteSpace returns True in this case. This is not shown in the program.
3. Whitespace only: The String data contains only whitespace. Any whitespace, including newlines and spaces, is considered.
VB.NET program that uses String.IsNullOrWhiteSpace
Module Module1
Sub Main()
' Test a Nothing String.
Dim test1 As String = Nothing
Dim test2 As String = " "
If String.IsNullOrWhiteSpace(test1) Then
Console.WriteLine(1)
End If
If String.IsNullOrWhiteSpace(test2) Then
Console.WriteLine(2)
End If
Dim test3 As String = "Sam"
If String.IsNullOrWhiteSpace(test3) Then
Console.WriteLine(3) ' Not reached.
End If
End Sub
End Module
Output
1
2
Info: IsNullOrEmpty has little or no performance impact. It does the minimal number of checks to perform its purpose.
But: In programs that only need to test the Length or against Nothing, it would be faster and clearer to instead do those checks alone.
Info: If this char-checking loop is not needed, IsNullOrEmpty would perform faster in a program.