VB.NET
Return String.IsNullOrEmpty(value) OrElse value.Trim().Length = 0
C#
return String.IsNullOrEmpty(value) || value.Trim().Length == 0;
Here are sample codes to use the new method:
VB.NET
Module Module1
Sub Main()
Dim samples() As String = {"Fryan", Nothing, String.Empty}
For Each sample As String In samples
Console.WriteLine(String.IsNullOrWhiteSpace(sample))
Next
Console.ReadKey()
End Sub
End Module
C#
class Program
{
static void Main(string[] args)
{
string[] samples = { "fryan", null, String.Empty };
foreach (string sample in samples)
Console.WriteLine(String.IsNullOrWhiteSpace(sample));
Console.ReadKey();
}
}
The above code should display a result of: False, True, True.
To stay up-to-date on Coding Tips & Tricks, subscribe now.
2 comments:
Thank you for your introduction but I can not understand where is the difference if I use string.IsNullOrEmpty(). The result gives back False, True, True too.
@rongchaua it may give the same result but the performance is a whole lot better!
Post a Comment