Link to home
Start Free TrialLog in
Avatar of Philippe Renaud
Philippe RenaudFlag for Canada

asked on

Help manipulating my String

Hello EE,

In reference to this:

https://www.experts-exchange.com/questions/27654805/Help-manipulating-my-String.html


I have a string:    "=SUM(B1+C1+D1)"

In the previous question I asked how to increase by 1 all the numbers to get B2+c2+D2 in the string... Thanks Kaufmed..

now I need to inscrease the letter... lets say I have X = 2

I would need to do:  index of B (wich would be 2 in alphabet)   + 2  equals 4

I would get:  D1,  E1 and F1

if string is:   "=SUM(A5+B5+C5)"  and   my X varialbe is 6

I would have:  "=SUM(G5 + H5 + I5)"


Can you help ?
Avatar of kaufmed
kaufmed
Flag of United States of America image

Try this mod to the original. Note:  because of your need to have a variable step between the letters, I had to introduce a "global" variable. This is replacementStep. You would need to set this variable prior to any call to Regex.Replace--at least any that use the ReplacementDelegate. I'd rather not have a global, but since the delegate definition for the third parameter to Regex.Replace dictates that the receiving method have one parameter, and that parameter be of type Match, I don't see another way to overcome this. If the step is later determined to be a fixed value, then you can remove the global and enter the fixed value for each occurrence of replacementStep within the ReplacementDelegate method.

Module Module1
    Private replacementStep As Integer

    Sub Main()
        replacementStep = 5

        Dim input As String = "=SUM(AA1+AZZ1+D1)"
        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

    Function ReplacementDelegate(ByVal m As System.Text.RegularExpressions.Match) As String
        Dim convertedLetters() As Char = m.Groups(1).Value.ToCharArray()
        Dim convertedNumber As Integer = Convert.ToInt32(m.Groups(2).Value)

        For i As Integer = convertedLetters.Length - 1 To 0 Step -1
            If i = convertedLetters.Length - 1 Then
                convertedLetters(i) = Chr(Asc(convertedLetters(i)) + replacementStep)
            End If

            If convertedLetters(i) > "Z"c Then
                convertedLetters(i) = "A"c
                convertedLetters(i - 1) = Chr(Asc(convertedLetters(i - 1)) + replacementStep)
            Else
                Exit For
            End If
        Next

        convertedNumber += 1

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

End Module

Open in new window

Avatar of Philippe Renaud

ASKER

Nice, almost done but I think there is a bug If I do:


 "=SUM(X1+Y1+Z1)"

can you try?
I concur. I'm looking into it.
I dont know if its the Z or my replacementValue that is 24   and the Char value on line :

convertedLetters(i) = Chr(Asc(convertedLetters(i)) + replacementStep)

gives me " [ "
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you Kaufmed.!!
Kaufmed, I just had an error with this code replacementStep is 26
Im receiving  "]"c  on line

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


mmm... want me to create a question ?
replacement was 52 sorry not 26

input was = C3-C4