O'Reilly logo

Microsoft® Visual Basic® .NET Programmer's Cookbook by Matthew MacDonald

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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() ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required