String IsNullOrEmpty. A String sometimes has no valid characters. The reference itself may be Nothing. And if a String does exist, it may be empty or have only whitespace characters.
These situations are detected with String.IsNullOrEmpty and IsNullOrWhiteSpace. We often use these functions in an If-Then statement.
IsNullOrEmpty. Here we use the String IsNullOrEmpty Function. This Function returns a Boolean. It checks the String argument against the Nothing constant. And it checks its Length as well.
And If the String reference is Nothing or the Length is zero it returns True. Otherwise it returns False.
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 ModuleTrue
Is empty
IsNullOrWhiteSpace. This program uses String.IsNullOrWhiteSpace. This Function checks also for the Nothing reference. But it checks instead for whitespace, not just an empty String.
So In other words, String.IsNullOrWhiteSpace will return True if any of these conditions matches.
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 Module1
2
Notes, IsNullOrEmpty. Internally, we find String IsNullOrEmpty is implemented in a straightforward way. It checks the String against null (Nothing) and checks the Length against zero.
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.
Notes, IsNullOrWhiteSpace. String IsNullOrWhiteSpace does more work when String data does exist. It scans over every character in the String to see if every character is whitespace.
Info If this char-checking loop is not needed, IsNullOrEmpty would perform faster in a program.
A summary. IsNullOrEmpty is similar to IsNullOrWhiteSpace. IsNullOrWhiteSpace checks the entire String for whitespace. If the string contains only whitespace, the Function returns True.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on May 19, 2023 (simplify).