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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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 ?
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)


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

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

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.