Solved

Help manipulating my String

Posted on 2012-03-30
9
215 Views
Last Modified: 2012-05-25
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
Comment
Question by:PhilippeRenaud
  • 6
  • 3
9 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37787469
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
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 37787915
Nice, almost done but I think there is a bug If I do:


 "=SUM(X1+Y1+Z1)"

can you try?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37788484
I concur. I'm looking into it.
0
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 37788493
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
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 37788678
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
 
LVL 1

Author Closing Comment

by:PhilippeRenaud
ID: 37788800
Thank you Kaufmed.!!
0
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 38012096
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
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 38012105
replacement was 52 sorry not 26

input was = C3-C4
0
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 38012386
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

827 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question