Reverse a String

Problem

You need to reverse the order of letters in a string.

Solution

Convert the string to an array of characters and use the Array.Reverse method, or use the legacy StrReverse Visual Basic 6 function.

Discussion

The functionality for reversing a string isn’t built into the String class, although it’s available in the Array class. Thus, one basic strategy for string reversal is to convert the string into an array of Char objects using the String.ToCharArray method. Then, you can reverse the array using the Array.Reverse shared method. Finally, you can create a new string using a special constructor that accepts a character array.

Dim Text As String = "The quick brown fox jumps over the lazy dog." Dim Chars() As Char = Text.ToCharArray() ...

Get Microsoft® Visual Basic® .NET Programmer's Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.