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 Truncate String

Use a Truncate Function to reduce the length of strings. Implement Truncate with Substring.
Truncate string. A string contains the letters "abcdef." But we just want the first 3 letters. We can truncate the string with a special Function.StringsSubstring
With an inner Substring call, we can implement string truncation. We must first check the desired length to ensure it will not cause Substring to throw an exception.
Example code. Here is our Truncate implementation. The Main Sub calls the Truncate Function with some example strings. It specifies a string and a length.

Argument 1: In our Truncate example Function, the first argument is the source string. This is what we take a substring from.

Argument 2: This is a length. This equals the count of characters we are truncating our string to.

VB.NET program that truncates Strings Module Module1 Public Function Truncate(value As String, length As Integer) As String ' If argument is too big, return the original string. ' ... Otherwise take a substring from the string's start index. If length > value.Length Then Return value Else Return value.Substring(0, length) End If End Function Sub Main() ' Test the Truncate method with these two strings. Dim test1 As String = "ABC" Dim test2 As String = "ABCDEF" Dim result = Truncate(test1, 2) Console.WriteLine("2=" + result) Console.WriteLine("4=" + Truncate(test2, 4)) Console.WriteLine("100=" + Truncate(test2, 100)) End Sub End Module Output 2=AB 4=ABCD 100=ABCDEF
A logical step. In Truncate, we have an If-statement. We cannot use Substring with a length that is beyond the end of our string. So we ensure our "length" argument is not too large.
A truncated summary. String truncation ensures a string does not exceed a certain number of characters. It does not provide padding to a specific character count.PadLeft, PadRight
© 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