Results 1 to 5 of 5

Thread: evaluating the contents of a string

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    6

    evaluating the contents of a string

    My app takes in a string from a text box which is of course located on a form.

    The contents of the string will be some form of expression statement

    EG (A*B)+(C/D)

    Once the user has entered their expression into the textbox and pressed a control button I want to be able to go though the string and split the contents into chars, oprators(*,+,-,/) and the parentheses.

    Is split the best way to do this? Ive been playing around with it and can't seem to find a way of picking out all chars (I could by divining a list of chars but that would take forever)


    Any ideas

  2. #2
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    So using the example expression you gave:

    You want....
    'EGABCD'
    '()()'
    '*+/'


    ?

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    6
    yes that is correct

  4. #4
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim s As String = TextBox1.Text
    3.         Dim characters As New System.Text.StringBuilder(15)
    4.         Dim paranthesis As New System.Text.StringBuilder(10)
    5.         Dim operators As New System.Text.StringBuilder(10)
    6.         Dim c As Char
    7.         For i As Integer = 0 To s.Length - 1
    8.             c = s.Chars(i)
    9.             If Char.IsLetterOrDigit(c) Then characters.Append(c)
    10.             If Char.IsPunctuation(c) Then
    11.                 If Not (c = "(" Or c = ")") Then
    12.                     operators.Append(c)
    13.                 Else
    14.                     paranthesis.Append(c)
    15.                 End If
    16.             End If
    17.             If Char.IsSymbol(c) Then operators.Append(c)
    18.         Next
    19.  
    20.         Debug.WriteLine("characters: " & characters.ToString)
    21.         Debug.WriteLine(Environment.NewLine)
    22.         Debug.WriteLine("paranthesis: " & paranthesis.ToString)
    23.         Debug.WriteLine(Environment.NewLine)
    24.         Debug.WriteLine("operators: " & operators.ToString)
    25.  
    26.     End Sub

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    6
    that is wicked - It never crossed my mind to use the char classes methods! thanks for the point in the right direction !-)

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