Solved

Help manipulating my String

Posted on 2012-03-30
9
203 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 74

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 74

Expert Comment

by:käµfm³d 👽
ID: 37788484
I concur. I'm looking into it.
0
 
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 74

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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 video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now