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 Trim Function

Use the Trim Function to remove leading and trailing whitespace from strings.
Trim removes leading and trailing whitespace. String data often has leading or trailing whitespace characters—newlines, spaces or tabs. These characters are usually not needed. With Trim we strip these characters in a declarative way.Strings
First example. This code references a string literal that contains leading spaces and a trailing space. Next the Trim Function is invoked, and a new string is allocated on the managed heap.

Tip: The string returned by the Trim Function does not contain the leading spaces or trailing space.

Next: To demonstrate, we print out the resulting value with square brackets denoting its first and last character positions.

VB.NET program that uses Trim function Module Module1 Sub Main() ' Input string. Dim value As String = " This is an example string. " ' Invoke Trim function. value = value.Trim() ' Write output. Console.Write("[") Console.Write(value) Console.WriteLine("]") End Sub End Module Output [This is an example string.]
File lines. Text files that you process in your VB.NET program may have unnecessary spaces or tab characters. If you read in a file line-by-line (as with File.ReadAllLines) you can then trim these lines and process them with simpler logic.File
VB.NET program that uses Trim on file lines Imports System.IO Module Module1 Sub Main() ' Read in the lines of a file. For Each line As String In File.ReadAllLines("data.txt") ' The existing line. Console.WriteLine("[{0}]", line) ' Trimmed line. Dim trimmed As String = line.Trim() Console.WriteLine("[{0}]", trimmed) Next End Sub End Module Output [ Something] [Something] [Example ] [Example] [ Another] [Another]
Dictionary. A popular use of the Dictionary constructed type is to store string keys and reference data objects as values. It is a good idea to trim input before passing it to a Dictionary lookup method.

Tip: This ensures that whitespace, which is typically not meaningful in this context, will not disrupt accurate lookups.

Dictionary
Summary. The Trim Function clears excess leading and trailing whitespace characters from string data. With Trim we more easily accept input from users and files. We can even look up keys in Dictionary or other data structure based on that input.
© 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