Reverse a String

Posted On 2018-01-20 by VB6Boy
Keywords:
Tags: VB6 String Handling Windows
Views: 44


This is a string handling function that that reverse a given string. For example, Good Morning becomes gninroM dooG.

The code reads a string into an array, which is no surprise. We could use a temporary "working" string, or an array to achieve the goal. What is a bit different here is that as the string is read in from the beginning, the values are placed at the back of the array.

The last step is the bring the array back out to a usable string, and return it to the caller.

Private Function ReverseString(ByRef StringToReverse As String) As String

    Dim lngIndex As Long
    Dim ByteArray() As Byte
    Dim tmpByte As Byte
    Dim lngMax As Long

    ByteArray = StrConv(StringToReverse, vbFromUnicode)
    lngMax = Len(StringToReverse) - 1

    For lngIndex = 0 To lngMax \ 2

        tmpByte = ByteArray(lngIndex)
        ByteArray(lngIndex) = ByteArray(lngMax - lngIndex)
        ByteArray(lngMax - lngIndex) = tmpByte

    Next

    ReverseString = StrConv(ByteArray, vbUnicode)

End Function


About the Author

VB6Boy has posted a total of 29 articles.


Comments On This Post

No comments on this post yet!


Do you have a thought relating to this post? You can post your comment here. If you have an unrelated question, you can use the Q&A section to ask it.

Or you can drop a note to the administrators if you're not sure where you should post.


Your IP address is:54.167.29.208

Before you can post, you need to prove you are human. If you log in, this test goes away.




Code Links