Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Help manipulating my String

Posted on 2012-03-30
9
Medium Priority
?
238 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 2000 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

Featured Post

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.

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

688 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