[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?
Re: splitting up a string
You just have to figure out how to parse it. Try this out:
VB Code:
Option Explicit
Private Sub Form_Load()
Dim str$
Dim a$()
Dim b As Integer
str = "User: /msg this is a message bla bla bla"
a() = Split(str, ": ")
b = InStr(a(1), " ")
MsgBox "User: " & a(0) & vbCrLf & _
Left(a(1), b) & vbCrLf & "Says: " & Mid(a(1), b + 1) & vbCrLf
End Sub
Re: splitting up a string
works perfect!
your the best, thanks
Re: [RESOLVED] splitting up a string
Glad that you are satisfied. :wave:
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?
Re: [RESOLVED] splitting up a string
VB Code:
Private Sub Form_Load()
Dim str$
Dim a$()
Dim b As Integer
str = "User: /msg this is a message bla bla bla"
a() = Split(str, ": ")
b = InStr(a(1), " ")
MsgBox "User: " & a(0) & vbCrLf & _
Left(a(1), b) & vbCrLf & "Says: " & Mid(a(1), b + 1) & vbCrLf
End Sub
You can say
VB Code:
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.