How to split "154*321/5+33-255"
to:
list1.additem "154"
list1.additem "*321"
list1.additem "/5"
list1.additem "+33"
list1.additem "-255"
?
Printable View
How to split "154*321/5+33-255"
to:
list1.additem "154"
list1.additem "*321"
list1.additem "/5"
list1.additem "+33"
list1.additem "-255"
?
Kars Lensen,
Use the Instr and Mid functions.
Here you go.VB Code:
Option Explicit Dim strEqu As String Private Sub Command1_Click() Dim i As Integer Do While Len(strEqu) > 0 For i = 2 To Len(strEqu) ' loop until you find a non numeric character If Not IsNumeric(Mid(strEqu, i, 1)) Then ' Add that portion to the listbox List1.AddItem Left$(strEqu, i - 1) ' Remove the portion just added to the ' listbox from the original string. strEqu = Mid(strEqu, i) ' Restart the for loop with the new string. Exit For End If ' When you reach the end of the string add ' whatever is left to the list box. If i = Len(strEqu) Then List1.AddItem strEqu strEqu = "" End If Next i Loop End Sub Private Sub Form_Load() strEqu = "154*321/5+33-255" End Sub
Thanks MarkT !!
Regards, K.
Just one problem: If there is a point (.) like 125*25/1.25 the number must appear:
125
*25
/1.25 ???!
Change: If Not IsNumeric(Mid(strEqu, i, 1)) Then
To: If (Not IsNumeric(Mid(strEqu, i, 1)) And (Mid(strEqu, i, 1) <> ".")) Then
so it treats "." as a numeric value
Thats it, thanks leinad31 !
What about handling for 10^2 and 1.90*102 (1.9E2)?
e.g. Powers and scientific form etc?
What are the limitations of the equation the user will enter?
s.
Depends on what the intended output is for those cases. But if there are more possibilities to consider then he'll probably use select case to handle them.
Probably something like..
VB Code:
Select Case Mid(strEqu, i, 1) Case "0" To "9" Case "." Case "^" Case Else End Select