Results 1 to 6 of 6

Thread: Find Values between 2 Characters

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    UK, Suffolk
    Posts
    318

    Find Values between 2 Characters

    I am looking to find away to get a value from between 2 characters

    example string would be "=CTL(1) + CTL(12) / 100"

    CTL(1) would equal 12 for example and CTL(12) would equal 34. So I need to return the string 12 + 34 / 100.

    Any ideas on how I can best achieve this?


    Thanks

    Dan

  2. #2
    Frenzied Member Bulldog's Avatar
    Join Date
    Jun 2005
    Location
    South UK
    Posts
    1,950

    Re: Find Values between 2 Characters

    What is "CTL" in this context?, is it an array?, a function? What type is CTL?

    When you say between two characters, do you mean the character that lies between two other characters with respect to the ASCII character table?


    • If my post helped you, please Rate it
    • If your problem is solved please also mark the thread resolved

    I use VS2015 (unless otherwise stated).
    _________________________________________________________________________________
    B.Sc(Hons), AUS.P, C.Eng, MIET, MIEEE, MBCS / MCSE+Sec, MCSA+Sec, MCP, A+, Net+, Sec+, MCIWD, CIWP, CIWA
    I wrote my very first program in 1979, using machine code on a mechanical Olivetti teletype connected to an 8-bit, 78 instruction, 1MHz, Motorola 6800 multi-user system with 2k of memory. Using Windows, I dont think my situation has improved.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    UK, Suffolk
    Posts
    318

    Re: Find Values between 2 Characters

    Each control located on a form implements a Interface with a property ControlID. In short the CTL standards for controlId, its that controlId I am trying the catch between the Brackets ()
    Last edited by drawlings; Mar 17th, 2015 at 11:29 AM.

  4. #4
    Frenzied Member Bulldog's Avatar
    Join Date
    Jun 2005
    Location
    South UK
    Posts
    1,950

    Re: Find Values between 2 Characters

    If the interface has ControlID as a property, you should be able to retrieve its value from Control.ControlID

    You could also use the Tag property to make your own ID and retrieve the value from Control.Tag


    • If my post helped you, please Rate it
    • If your problem is solved please also mark the thread resolved

    I use VS2015 (unless otherwise stated).
    _________________________________________________________________________________
    B.Sc(Hons), AUS.P, C.Eng, MIET, MIEEE, MBCS / MCSE+Sec, MCSA+Sec, MCP, A+, Net+, Sec+, MCIWD, CIWP, CIWA
    I wrote my very first program in 1979, using machine code on a mechanical Olivetti teletype connected to an 8-bit, 78 instruction, 1MHz, Motorola 6800 multi-user system with 2k of memory. Using Windows, I dont think my situation has improved.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    UK, Suffolk
    Posts
    318

    Re: Find Values between 2 Characters

    Sorry maybe I didnt explain myself.

    I have a 'string' with the following value "CTL(1) + CTL(12) / 100" " (retrieved from a database). so basically a simple formula.

    once I can extract values between CTL() I can then retrieve the value from ControlId. Until that point it is just a string.

    Once I have the values I then use NCALC the calculate the expression.

    I've managed a way to do it using Substring, but to be honest it looks rubbish But i can continue until I find a better solution

    Code:
      Private Shared tempString As String
    
            Public Shared Function ConvertFormula(formula As String, _
                                                  parentForm As Control) As String
                Try
    
    
    
                    Dim s As String
    
                    s = formula.ToUpper
    
                    If s.Length = 0 Then
                        Return String.Empty
                    End If
    
                    Dim flag As Boolean = True
                    Dim tStr As Integer
                    Dim nStr As String = String.Empty
    
                    Do While flag = True
    
                        tStr = InStr(s, "CTL(")
    
                        If tStr > 1 Then ' 1 is always the first index Char
                            nStr &= s.Substring(0, tStr - 1)
                            ' Remove Excess from the beginning of the string
                            s = s.Substring(tStr - 1, s.Length - (tStr - 1))
                            tStr = InStr(s, "CTL(")
                        ElseIf tStr = 0 Then
                            nStr &= s
                            flag = False
                            Exit Do
                        End If
                        ' Remove CTL(
                        s = s.Substring(tStr + 3, s.Length - 4)
                        ' Find the Close Bracket
                        tStr = InStr(s, ")")
    
                        tempString = String.Empty
    
                        Call FindControlValue(CInt(s.Substring(0, tStr - 1)), parentForm)
    
                        If String.IsNullOrEmpty(tempString) Then
                            Throw New NoDataToCalculateException
                        End If
    
                        nStr &= CStr(tempString) '& s.Substring(tStr, s.Length - tStr)
    
                        If String.IsNullOrEmpty(nStr) Then
                            Throw New NoDataToCalculateException
                        End If
    
                        s = s.Substring(tStr, s.Length - tStr)
    
                    Loop
    
                    Return nStr
                Catch ex As NoDataToCalculateException
                    Throw New NoDataToCalculateException
                Catch ex As Exception
                    errorMessages.Message(ex.Message, "Convert Formula")
                    Return String.Empty
                End Try
            End Function
    
            Private Shared Sub FindControlValue(controlID As Integer, _
                                                    parentForm As Control)
                For Each item As Control In parentForm.Controls
                    If TypeOf item Is IControlSQLSupport Then
                        If DirectCast(item, IControlSQLSupport).ControlId = controlID Then
                            tempString = DirectCast(item, IControlSQLSupport).Text
                        End If
                    End If
    
                    If item.HasChildren Then
                        FindControlValue(controlID, item)
                    End If
                Next
            End Sub

  6. #6
    Frenzied Member Bulldog's Avatar
    Join Date
    Jun 2005
    Location
    South UK
    Posts
    1,950

    Re: Find Values between 2 Characters

    Ah now I understand.

    If that's a string then you could split on the brackets like this;
    Code:
    Dim str As String = "CTL(1) + CTL(12) / 100"
    Dim fields As String() = str.Split(New Char() {"(", ")"}, StringSplitOptions.RemoveEmptyEntries)
    and then work out what to do with the fields. field(0) will be "CTL", field[1] will be "1" and so on...

    You could also pull out the bracketed numbers with a regular expression;

    Code:
    Imports System.Text.RegularExpressions
    Public Class Form1
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) _
        Handles MyBase.Load
    
            Dim str As String = "CTL(1) + CTL(12) / 100"
    
            Dim r As New Regex("(?<=\().*?(?=\))")
            For Each m As Match In r.Matches(str)
                MessageBox.Show(m.ToString())
            Next
    
        End Sub
    End Class
    Last edited by Bulldog; Mar 17th, 2015 at 12:53 PM.


    • If my post helped you, please Rate it
    • If your problem is solved please also mark the thread resolved

    I use VS2015 (unless otherwise stated).
    _________________________________________________________________________________
    B.Sc(Hons), AUS.P, C.Eng, MIET, MIEEE, MBCS / MCSE+Sec, MCSA+Sec, MCP, A+, Net+, Sec+, MCIWD, CIWP, CIWA
    I wrote my very first program in 1979, using machine code on a mechanical Olivetti teletype connected to an 8-bit, 78 instruction, 1MHz, Motorola 6800 multi-user system with 2k of memory. Using Windows, I dont think my situation has improved.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width