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 TrimEnd and TrimStart Examples

Use the TrimEnd and TrimStart Functions on String instances.
TrimEnd removes characters from the end of a String. With it we specify an array of Chars we want to remove. Then the returned String value has those particular Chars removed if they fall at the end.Strings

Note: TrimEnd receives a variable number of arguments. We pass them directly with no special syntax.

TrimEnd. In this example, we create an array of two Strings and then loop over it. We call TrimEnd on each element in the array. There are three arguments to TrimEnd, which are the punctuation and whitespace characters we want to remove.For Each, For

Note: You can see that the elements in the String array have had certain characters at their ends removed.

VB.NET program that uses TrimEnd function Module Module1 Sub Main() ' Input array. Dim array(1) As String array(0) = "What's there?" array(1) = "He's there... " ' Use TrimEnd on each String. For Each element As String In array Dim trimmed As String = element.TrimEnd("?", ".", " ") Console.WriteLine("[{0}]", trimmed) Next End Sub End Module Output [What's there] [He's there]
TrimStart. TrimStart removes first characters from a String. With TrimStart we specify an array of Chars to be removed. TrimStart then scans the String and removes any characters found. It handles not just whitespace but any character value.

Here: We remove two Char values from the String: the period and the space. These are part of a Character array.

CharChar Array

Note: In the String literal, there are three periods and a space. All four of those characters are removed from the beginning.

VB.NET program that uses TrimStart Module Module1 Sub Main() Dim text As String = "... The Dev Codes" Dim array(1) As Char array(0) = "." array(1) = " " text = text.TrimStart(array) Console.WriteLine("[{0}]", text) End Sub End Module Output [The Dev Codes]
Array. To trim different characters from the beginning of the String, you can change the array passed to TrimStart. You can trim any character value you wish, even regular letters such as "a" through "z". The function is case-sensitive.
Optimization. The example shows an inefficient way to use TrimEnd. When you call TrimEnd as shown above, a new array is created on the heap each time the method is called. With clever programming, this is avoided.

Tip: You can create the Char array outside of the loop, or make it a Shared field. Then pass a reference to the existing array to TrimEnd.

Summary. TrimEnd and TrimStart are often useful. They can be helpful in programs that process text in complicated ways. Sometimes, these methods are easier to use than regular expressions—the pattern is specified as a Char array.
© 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