Results 1 to 9 of 9

Thread: [RESOLVED] split string

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 1999
    Location
    Ruinen, Drente, Netherlands
    Posts
    192

    Resolved [RESOLVED] split string

    How to split "154*321/5+33-255"
    to:
    list1.additem "154"
    list1.additem "*321"
    list1.additem "/5"
    list1.additem "+33"
    list1.additem "-255"
    ?

  2. #2
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385
    Kars Lensen,

    Use the Instr and Mid functions.

  3. #3
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141
    Here you go.
    VB Code:
    1. Option Explicit
    2. Dim strEqu As String
    3.  
    4. Private Sub Command1_Click()
    5. Dim i As Integer
    6.    
    7.     Do While Len(strEqu) > 0
    8.         For i = 2 To Len(strEqu)
    9.             ' loop until you find a non numeric character
    10.             If Not IsNumeric(Mid(strEqu, i, 1)) Then
    11.                 ' Add that portion to the listbox
    12.                 List1.AddItem Left$(strEqu, i - 1)
    13.                 ' Remove the portion just added to the
    14.                 ' listbox from the original string.
    15.                 strEqu = Mid(strEqu, i)
    16.                 ' Restart the for loop with the new string.
    17.                 Exit For
    18.             End If
    19.            
    20.             ' When you reach the end of the string add
    21.             ' whatever is left to the list box.
    22.             If i = Len(strEqu) Then
    23.                 List1.AddItem strEqu
    24.                 strEqu = ""
    25.             End If
    26.         Next i
    27.     Loop
    28. End Sub
    29.  
    30. Private Sub Form_Load()
    31.     strEqu = "154*321/5+33-255"
    32. End Sub

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Apr 1999
    Location
    Ruinen, Drente, Netherlands
    Posts
    192
    Thanks MarkT !!

    Regards, K.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Apr 1999
    Location
    Ruinen, Drente, Netherlands
    Posts
    192
    Just one problem: If there is a point (.) like 125*25/1.25 the number must appear:

    125
    *25
    /1.25 ???!

  6. #6
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    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

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Apr 1999
    Location
    Ruinen, Drente, Netherlands
    Posts
    192
    Thats it, thanks leinad31 !

  8. #8
    Registered User
    Join Date
    Feb 2003
    Posts
    57
    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.

  9. #9
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    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:
    1. Select Case Mid(strEqu, i, 1)
    2. Case "0" To "9"
    3.  
    4. Case "."
    5.  
    6. Case "^"
    7.  
    8. Case Else
    9. End Select

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