• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 340
  • Last Modified:

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.

Brad
0
bradbritton
Asked:
bradbritton
1 Solution
 
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 ?
0
 
Arthur_WoodCommented:
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)

AW
0
 
Brian CroweCommented:
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))
            Next

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

then just reconstitute the string by concatenating the array members in reverse order
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Fernando SotoCommented:
Hi bradbritton;

This line of code will do what you want.

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


Fernando
0
 
ericwong27Commented:

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
        Next

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

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


    End Function
0
 
Arthur_WoodCommented:
glad to be of assistance

AW
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now