How do I change order in a string? This is not a true "reverse" function

Posted on 2006-04-03
Last Modified: 2010-05-18
I have a string that reads "34D16C", which I need to reverse to to read 6CD134. I tried to split this into an array , then rebuild the string, but I always seem to get the reverse of the string and not what I am looking for. Thanks in advance.

Question by:bradbritton
    LVL 52

    Expert Comment

    by:Carl Tawn
    I don't quite understand. "6CD134" isn't the reverse of "34D16C", but you say that the reverse is what you need. Then you say you only manage to get the reverse, which isn't what you wanted. Is there a pattern to how you need to rearrange the string ?
    LVL 44

    Accepted Solution

    so let me see if I understand what you seem to be asking

    If the original string was "123456" what you seem to want is to reorder the characters as "563412"

    so the characters that were in positions 5 and 6 are swapped with the characters that were originally in positions 1 and 2.  Is that correct?

    try this:

            Dim MyString As String = "6CD134"
            Dim NewString As String = MyString.Substring(4, 2) & MyString.Substring(2, 2) & MyString.Substring(0, 2)

    LVL 34

    Expert Comment

    by:Brian Crowe
    Here is a function that will split the string up into 2-character strings

    Private Function SplitPairs(ByVal input As String) As String()

                Dim alPairs As New ArrayList()
                Dim i As Integer

                For i = 0 To input.Length - 2 Step 2
                      alPairs.Add(input.Substring(i, 2))

                Return alPairs.ToArray(GetType(String))
          End Function

    then just reconstitute the string by concatenating the array members in reverse order
    LVL 62

    Expert Comment

    by:Fernando Soto
    Hi bradbritton;

    This line of code will do what you want.

        Dim output As String = System.Text.RegularExpressions.Regex.Replace( _
                input, "(..)(..)(..)", "$3$2$1")

    LVL 10

    Expert Comment


    Enhance from code provided by Fernando :-)

    Reverse('6CD134', 1)
    6CD134 -> 431DC6

    Reverse('6CD134', 2)
    6CD134 -> 34D16C

    Reverse('6CD134', 3)
    6CD134 -> 1346CD

      Private Function Reverse(ByVal input As String, ByVal totalWidth As Integer) As String

            ' Invalid parameter
            If String.IsNullOrEmpty(input) Then Return input
            If totalWidth >= input.Length Then Return input

            ' Reverse String Pattern
            Dim pattern As String = "(" & ".".PadRight(totalWidth, "."c) & ")"
            Dim length As Integer = Int(input.Length / totalWidth)

            Dim reverseInput As String = input.Substring(0, totalWidth * length)

            Dim reversePattern As String = String.Empty
            For i As Integer = 1 To length
                reversePattern += pattern

            Dim replacement As String = String.Empty
            For i As Integer = length To 1 Step -1
                replacement += "$" + i.ToString

            Return System.Text.RegularExpressions.Regex.Replace(input, reversePattern, replacement)

        End Function
    LVL 44

    Expert Comment

    glad to be of assistance


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now