TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to VBNET

VB.NET String.IsNullOrEmpty, IsNullOrWhiteSpace

Use the String.IsNullOrEmpty and String.IsNullOrWhiteSpace Functions to test strings.
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.If ThenStrings
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.NothingString Length

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
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.

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
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.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf