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

Reverse the characters in Strings using ToCharArray and Array.Reverse.
Reverse String. A String contains individual characters. The order of these characters can be reversed. In VB.NET we reverse a string using the ToCharArray function. We then use the Array.Reverse Sub.Strings
Example. Here we define a function Reverse that accepts on String argument and returns a String. Then we store the value returned by ToCharArray in a local variable. Next pass that variable to the Array.Reverse subroutine.Array

Info: Reverse internally rearranges the characters in the array to be in the opposite order.

Finally: You must invoke the String constructor. This converts the reversed character array back into a string.

VB.NET program that reverses strings Module Module1 Sub Main() ' Test. Console.WriteLine(Reverse("Codex")) Console.WriteLine(Reverse("sam")) Console.WriteLine(Reverse(Reverse("Codex"))) End Sub ''' <summary> ''' Reverse input string. ''' </summary> Function Reverse(ByVal value As String) As String ' Convert to char array. Dim arr() As Char = value.ToCharArray() ' Use Array.Reverse function. Array.Reverse(arr) ' Construct new string. Return New String(arr) End Function End Module Output slrep mas Codex
Interviews. Readers have commented that in interview questions, the classic question of writing a method to reverse a string often have some constraints. For example, you might be required not to call methods such as Array.Reverse.

However: Even with these constraints, you would likely need to use the String constructor or possibly ToCharArray.

ToCharArray
Summary. It has little practical application in real applications. But learning to reverse a string in the VB.NET language is both informative and elucidating. Strings in VB.NET cannot be manipulated directly. They are immutable.

Instead: You must convert them to character arrays with the ToCharArray function.

© 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