Link to home
Start Free TrialLog in
Avatar of ucla11
ucla11

asked on

Trimming the last character in a string

I have two text boxes on a form ...one for the base number and the other for the exponent.  If the user enters 3 intxtBase,text and 3 in txtExp.text that is 3^3  I am using a for next loop to convert that into its exapaned form...3X3X3


Dim base as Integer
DimBase As Interger
Dim x As Integer

Base= Val(txtBase.Text)
Exp = Val(txtExp.Text)

For x = 1 to Exp

'third textbox on form to display the expanded version
txtAnwser.Text = txtAnwser.Text & Base & " X"

Next x

I know I will have on extra "X" how to I trim this extra "X" off and will this loop even work and give  the result in the textBox:
3X3X3 ....?
SOLUTION
Avatar of Bob Lamberson
Bob Lamberson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ucla11
ucla11

ASKER

Both worked great How about doing the opposite lets say thet type in 3X3X3X3 in the  txtAnswer.Text  textbox and in  txtBase.Text shows a 3 and in
    txtExp.Text shows a  4
   
Private Sub Command2_Click()
    Dim temp As Variant
    Dim base As String
    Dim exp As String
    Dim i As Integer
    Dim tempBase As String
   
    temp = Split(txtAnswer.Text, "X")
    For i = LBound(temp) To UBound(temp)
        If temp(i) = "" Then
            MsgBox "Please try again", vbInformation, "Invalid Format"
            Exit Sub
        ElseIf tempBase = "" Then
            tempBase = temp(i)
        Else
            If temp(i) <> tempBase Then
                MsgBox "Please try again", vbInformation, "Invalid Format"
                Exit Sub
            End If
        End If
    Next i
    txtBase = tempBase
    txtExp = UBound(temp) + 1
End Sub
Avatar of ucla11

ASKER

If I enter 3X3X3  in the txtAnswer.Text
The code does not work quite right inthe txtBase it diplys 3X3X3 and in txtExp it displays 1(Always 1)
Make sure you are using an uppercase "X" and that there are no spaces.  You could repace this line:    

    temp = Split(txtAnswer.Text, "X")

with this to make it a little safer:

    temp = Split(UCase(Trim(txtAnswer.Text)), "X")

The code works perfectly on my system though.

~IM
Avatar of ucla11

ASKER

Thanks, I was typing in lowercase x.............