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

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.

Who is Participating?
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)

Carl TawnSystems and Integration DeveloperCommented:
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 ?
Brian CroweDatabase AdministratorCommented:
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
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Fernando SotoRetiredCommented:
Hi bradbritton;

This line of code will do what you want.

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


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
glad to be of assistance

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.