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

Use the String.Empty property. The empty string has length of zero but is not null.
Empty. An empty string has length 0. It is not null. We can specify in VB.NET an empty string with "" but the String.Empty property is easier to read. It may be better for code.
In this example, we introduce a string variable called "value." We assign it to the String.Empty property. Then we test it against "": this check evaluates to true.

IsNullOrEmpty: We use the IsNullOrEmpty method. This method correctly detects the empty string.

String.IsNullOrEmpty

Length: Finally we access the Length property of the string. This returns, as expected, the value 0.

String Length
VB.NET program that uses String.Empty Module Module1 Sub Main() ' Assign a string to String.Empty. Dim value As String = String.Empty ' It equals an empty literal. If value = "" Then Console.WriteLine(1) End If ' It is detected by IsNullOrEmpty. If String.IsNullOrEmpty(value) Then Console.WriteLine(2) End If ' It has length 0. Console.WriteLine(value.Length) End Sub End Module Output 1 2 0
There is no discernible difference between using String.Empty and an empty literal. The main difference comes from the appearance, and readability, of code.

Tip: Passing String.Empty to a Function call is often more readable. But this comes down to your preference.

String.Empty is a shared property. It is not a constant, so it has a tiny difference with an empty literal. A load must occur to read it.Shared

But: For almost all programs, no difference is going to be detected. For maximum performance, I recommend directly using the empty literal.

IsNullOrWhiteSpace. A string with only whitespace is not empty. But in some programs we might want to test for whitespace-only or null strings. IsNullOrWhiteSpace helps here.
A review. Empty is a handy property to know about: it can make some code more self-documenting. The empty literal may be more mysterious. String.Empty clearly indicates its contents.
© 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