Reverse

tk.unlimited

Active member
Joined
Apr 2, 2005
Messages
27
Programming Experience
1-3
Hey ALL!!

Im having troubles with Reversing text from a text box. e.g If the sentence is Hello there then ereht olleH is displayed.. To use the LEN function and accessing a single character with sSentence.chars(icounter) <--- From For LOOP

Tk Unlimited!!
 
Btw, if you wish to flip around the front and back end of a string, then the StrReverse(string) is for you.

next code is reversing the string:
VB.NET:
Dim strRev AsString = StrReverse("Hello there")
 
MessageBox.Show(strRev)

or Simply

VB.NET:
MessageBox.Show(StrReverse("Hello there"))


Cheers ;)
 
strange, StrReverse is a member of the same class as the Len function (Microsoft.VisualBasic.Strings) :confused: .

If you wish not to use the Microsoft.VisualBasic namespace (which is up to debate), here's a simple recursive function:
VB.NET:
Function ReverseString(ByVal str As String) As String
    If str.Length = 1 Then
        Return str
    Else
        Return ReverseString(str.Substring(1)) + str.Substring(0, 1)
    End If
End Function
 
I'm in favour of using System-based alternatives to VB.NET Runtime functions if they exist. Given that there is no direct alternative to StrReverse I would recommend using it in this case. If you do want to avoid Runtime functions, here's a non-recursive alternative that avoids excessive string manipulation:
VB.NET:
	Public Function StrReverse(ByVal str As String) As String
		Dim chars As Char() = str.ToCharArray()

		Array.Reverse(chars)
		Return New String(chars)
	End Function
 
Back
Top