Results 1 to 4 of 4

Thread: Take the left part of a math question, and cut off the right par after the = sign.

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Take the left part of a math question, and cut off the right par after the = sign.

    Hi there! I am working on a math review game for teachers. Basically, they come up with a list of math questions ex: 1+1=2. What I would like to do, is have the program cut off the question and put it in QuestionLBL.caption

    In this case would be 1+1, and the answerLBL.caption would be 2. So I am looking for a way to cut off at the equal sign.

    Would anyone know how to do that?

    Thanks!

  2. #2
    PowerPoster
    Join Date
    Aug 2010
    Location
    Canada
    Posts
    2,896

    Re: Take the left part of a math question, and cut off the right par after the = sign

    You can use the InStr function to return the position of the equal sign, then use Left$ to get the leftmost portion, and Mid$ to get the rightmost portion.

    For example:

    Code:
    Public Sub SplitEquation(ByVal p_Equation As String)
       Dim l_EqualPos As Long
    
       l_EqualPos = InStr(p_Equation, 1, "=")
    
       If l_EqualPos < 1 Then Err.Raise 5, , "Not an equation!"
    
       Me.questionLBL.Caption = Left$(p_Equation, l_EqualPos)
       Me.answerLBL.Caption = Mid$(p_Equation, l_EqualPos + 1)
    End Sub
    Note the above was written off the top of my head (untested in IDE), so there may be errors. It should point you in the right direction though.

  3. #3
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: Take the left part of a math question, and cut off the right par after the = sign

    The Split function could be your friend here.
    Code:
    Dim strEquation As String
    Dim strQuestion As String
    Dim strAnswer As String
    Dim strParts() As String
    
        strEquation = "1 + 1 = 2"
        
        strParts = Split(strEquation, "=")
        
        strQuestion = Trim(strParts(0))
        strAnswer = Trim(strParts(1))
        
        MsgBox "Question:   " & strQuestion & vbCrLf & "Answer:   " & strAnswer

  4. #4
    PowerPoster
    Join Date
    Aug 2010
    Location
    Canada
    Posts
    2,896

    Re: Take the left part of a math question, and cut off the right par after the = sign

    Even better! Should remember not to post before my first coffee

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