Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 242
  • Last Modified:

Help manipulating my String

Hello EE,

In reference to this:

http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_27654805.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 ?
0
PhilippeRenaud
Asked:
PhilippeRenaud
  • 6
  • 3
1 Solution
 
käµfm³d 👽Commented:
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

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


 "=SUM(X1+Y1+Z1)"

can you try?
0
 
käµfm³d 👽Commented:
I concur. I'm looking into it.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
PhilippeRenaudAuthor Commented:
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 " [ "
0
 
käµfm³d 👽Commented:
I believe this has it sorted out.

Module Module1
    Private replacementStep As Integer

    Sub Main()
        replacementStep = 5

        Dim input As String = "=SUM(YZ1+Z1+ABC1+XZZ1+XYZ1+Y10)"
        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
        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
End Module

Open in new window

0
 
PhilippeRenaudAuthor Commented:
Thank you Kaufmed.!!
0
 
PhilippeRenaudAuthor Commented:
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 ?
0
 
PhilippeRenaudAuthor Commented:
replacement was 52 sorry not 26

input was = C3-C4
0
 
PhilippeRenaudAuthor Commented:
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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