Developer

How to reverse a string in VB.NET

In this week's VB.NET tip, Irina Medvinskaya shows you how to utilize the Reverse method of an array to reverse the order of characters in a particular string.

There are situations that require reversing the order of characters in a string. In the Listing A example, I show you how to utilize the Reverse method of an array to reverse the order of characters in a particular string.

I initially define two string variables: strOriginalText (to hold the initial text we define) and strReversedText (to hold the value of the strOriginalText in the reverse order). The value of strOriginalText is set to "original text."  In order to reverse the value of the text in strOriginalText, I create a character array arrChar and set it to the value of strOriginalText.ToCharArray() method, which allows me to take the value of the strOriginalText and convert it into the array of characters. I then reverse the value of the arrChar by utilizing the Reverse method of the Array class. The resulting value is assigned to the strReversedText string and displayed in the MessageBox.

Miss a tip?

Check out the Visual Basic archive, and catch up on the most recent editions of Irina Medvinskaya's column.

Advance your scripting skills to the next level with TechRepublic's free Visual Basic newsletter, delivered each Friday. Automatically sign up today!

Editor's Picks

Free Newsletters, In your Inbox