Help manipulating a String

PhilippeRenaud
PhilippeRenaud used Ask the Experts™
on
Hello EE, I have this code:

    Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        replacementStep = 5

        Dim input As String = "=C3-C4"
        Dim result As String = System.Text.RegularExpressions.Regex.Replace(input, "([A-Z]+)(\d+)", AddressOf ReplacementDelegate)

        Console.WriteLine(input)
        Console.WriteLine(result)
        'Console.ReadKey()
    End Sub

Open in new window



    Private Function ReplacementDelegate(ByVal m As System.Text.RegularExpressions.Match) As String
        Const Letter_Z As Char = "Z"c
        Const Letter_A As Char = "A"c

        Dim convertedLetters() As Char = m.Groups(1).Value.ToCharArray()

        convertedLetters(convertedLetters.Length - 1) = Chr(Asc(convertedLetters(convertedLetters.Length - 1)) + replacementStep)

        ' Adjust for Zs
        For i As Integer = convertedLetters.Length - 1 To 0 Step -1
            If convertedLetters(i) > Letter_Z Then
                convertedLetters(i) = Chr((Asc(Letter_A) - 1) + (Asc(convertedLetters(i)) Mod Asc(Letter_Z)))

                If i = 0 Then
                    Dim temp(convertedLetters.Length) As Char

                    Array.Copy(convertedLetters, 0, temp, 1, convertedLetters.Length)
                    convertedLetters = temp
                    convertedLetters(0) = Letter_A

                    Exit For
                Else
                    convertedLetters(i - 1) = Chr(Asc(convertedLetters(i - 1)) + 1)
                End If
            Else
                Exit For
            End If
        Next

        Dim convertedNumber As Integer = Convert.ToInt32(m.Groups(2).Value)

        convertedNumber += 1

        Return (New String(convertedLetters) + convertedNumber.ToString())
    End Function

Open in new window


you will see that by changing the value replacementStep it will change the letter in the output .. from  =C3-C4 to lets say =H4-H5   etc..

if you do replacementStep to 49   it will be:  =AZ4-AZ5   because after 26 its like in excel you increase a letter...  but after 49 I should get =BA4-BA5 or more  but instead im having =A]4-A]5   this is wrong

this code comes from Kaufmed. Its a new question for him but if anyone wnats to try to help...

thanks.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
Sorry for the delay. I cannot check my personal email while at work, so I didn't see your previous posts until I took lunch  = )

As to your issue, is "=C3-C4" the value you are testing step=49 with? I get this:

Screenshot
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
Never mind. I misinterpreted the question. Be back in a minute  = )

Author

Commented:
alright
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
Commented:
Took a bit longer than I expected, but I tested this against a macro I created in Excel and it appears to be correct now.

Private Function ReplacementDelegate(ByVal m As System.Text.RegularExpressions.Match) As String
    Const Letter_Z As Integer = Asc("Z"c)
    Const Letter_A As Integer = Asc("A"c)

    Dim letters() As Char = m.Groups(1).Value.ToCharArray()
    Dim lastIndex As Integer = letters.Length - 1
    Dim toAdd As Integer = replacementStep - 1
    Dim didOverflow As Boolean = False

    For i As Integer = lastIndex To 0 Step -1
        Dim tempChar As Integer = Asc(letters(i)) + toAdd + 1
        Dim overflow As Integer = tempChar - Letter_Z

        If overflow > 0 Then
            Dim offsetFromA As Integer = ((overflow Mod 26) - 1)

            letters(i) = Chr(Letter_A + offsetFromA)
            toAdd = (overflow \ 26)
            didOverflow = True
        Else
            letters(i) = Chr(tempChar)
            toAdd = 0
            didOverflow = False
        End If
    Next

    If didOverflow Then
        Dim temp(letters.Length) As Char

        Array.Copy(letters, 0, temp, 1, letters.Length)
        letters = temp
        letters(0) = Chr(Letter_A + toAdd)
    End If


    Dim convertedNumber As Integer = Convert.ToInt32(m.Groups(2).Value)

    convertedNumber += 1

    Return (New String(letters) + convertedNumber.ToString())
End Function

Open in new window


The Excel macro (for comparison):

Sub Macro1()

    ActiveCell.Offset(1, 200).Select

End Sub

Open in new window


You can fiddle with the "200" in the macro to simulate different "replacementStep" values.

Author

Commented:
Amazing Kaufmed. thank you a lot.
no worries for the time!
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
Glad to help  = )

Author

Commented:
Kaufmed, I think I found a little bug...

lets say string is =AA15 + AA17

if replcementStep = 0, result will be =BA16 + BA18

but at 0 it should not increase letter... (like it does well if string is:  =A1+A2  result will be:  =A2+A3)

and also... it should increment to AB and not BA

want me to create a new question for that?
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
Nah. Just ad the following at line 5:

If replacementStep <= 0 Then
    Return m.Value
End If

Open in new window

Author

Commented:
Kaufmed, new question regarding this one... 500 points...

ive found 2 problems... I hope you can help soon...  : /



http://www.experts-exchange.com/Programming/Languages/.NET/Q_27780284.html

thanks a lot

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial