Results 1 to 6 of 6

Thread: [RESOLVED] splitting up a string

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2001
    Posts
    154

    Resolved [RESOLVED] splitting up a string

    Ill tell you what i need it for first. It is a simple chat that looks for a command in text that is sent. for instance "/msg"

    Now each string will consist of this ( User: /msg this is a message bla bla bla )
    what i need to do is split this up so that it will put the following into seperate string:

    User
    Command ( /msg for example )
    and what follows /msg

    i've tried using the split function but i dont know how to make everything that follows /msg a complete string instead after each space it is broken up

    heres the code for that which wont work for me


    a = Split(teststring)
    For i = LBound(a) To UBound(a)

    MsgBox a(i)
    Next
    End

    i need something with Len correct?
    Last edited by Coolone770; Dec 7th, 2005 at 12:17 AM.

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: splitting up a string

    You just have to figure out how to parse it. Try this out:
    VB Code:
    1. Option Explicit
    2.  
    3.  
    4. Private Sub Form_Load()
    5.   Dim str$
    6.   Dim a$()
    7.   Dim b As Integer
    8.   str = "User: /msg this is a message bla bla bla"
    9.   a() = Split(str, ": ")
    10.   b = InStr(a(1), " ")
    11.   MsgBox "User: " & a(0) & vbCrLf & _
    12.     Left(a(1), b) & vbCrLf & "Says: " & Mid(a(1), b + 1) & vbCrLf
    13. End Sub

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2001
    Posts
    154

    Re: splitting up a string

    works perfect!
    your the best, thanks

  4. #4
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: [RESOLVED] splitting up a string

    Glad that you are satisfied.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Nov 2001
    Posts
    154

    Re: [RESOLVED] splitting up a string

    is there a way to verify the string is in this format before breaking it down so it doesnt return any errors?

  6. #6
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: [RESOLVED] splitting up a string

    VB Code:
    1. Private Sub Form_Load()
    2.   Dim str$
    3.   Dim a$()
    4.   Dim b As Integer
    5.   str = "User: /msg this is a message bla bla bla"
    6.   a() = Split(str, ": ")
    7.   b = InStr(a(1), " ")
    8.   MsgBox "User: " & a(0) & vbCrLf & _
    9.     Left(a(1), b) & vbCrLf & "Says: " & Mid(a(1), b + 1) & vbCrLf
    10. End Sub

    You can say
    VB Code:
    1. if Instr(str,":") > 0 then ' make sure there's a ":"
    then, as long as there's a space after that, you're cool.
    If not, you could do the same thing, but I think you'd find a space anyways.

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