Pulling Apart A String into diferent strings (and some integers) (VB6)


my program reads a text file outputted by another program and places it into a string i need to split this string up into seperate strings how would this be done?

One thing that the string may contain is

[02:10:13] Host: DrkAgnt is a game moderator.

this is basically what someone is saying in game what i need to split it up into is..

1st word
2nd word
3rd word
4th word
rest of there message
chat mode

so the above would be split into the following

Time = 02:10:13
nick = Host
1st word = DrkAgnt
2nd word = is
3rd word = a
4th word = game
rest of text = moderator.
chat mode = ALL

you can tell what chat mode would be by the following

Page would be

[00:37:41] [Page] ooops21: OK

(note the [Page] part of it)

Team would be

[00:37:41] [Team] ooops21: OK

and All would be

[00:37:41] ooops21: OK

it cane also comes out like the following...

[02:05:41] Id  Name           Score Side      Ping      Address               Kbits/s Time
[02:05:41]   1 iso8859        267   GDI      135;1068    19      000.06.04
[02:05:41]   2 Weslox2        628   NOD      163;1612     23      000.04.49
[02:05:41]   3 aaa11888       12    GDI      118;30     19      000.01.41
[02:05:41]   4 hex29          0     NOD      109;2647    16      000.01.34

this will come one string at a time (im using a For t = 1 To foundcount        Next loop )

i need this split into the following

Private Type player_info(127)
name As String
Score As Integer
Side As String
Ping As Integer
IP As String
Port As Integer
kb As Integer
hour As Integer (the 3 digits under time)
minute As Integer (the next 2)
seconds As Integer (the last 2)
End Type

i need the id to be same as the array number if there is no id for a certain array number it needs to be blank

sometimes it could also come out like...

[02:07:47] Westwood Online mode active since 4/11/2004 - 14:45:51 PM
[02:07:47] Gameplay in progress
[02:07:47]      Map : C&C_Canyon.mix
[02:07:47]     Time : 0.28.23
[02:07:47]      Fps : 60
[02:07:47]      GDI : 1/32 players      1059 points
[02:07:47]      NOD : 2/32 players      2618 points


[02:07:47] GameSpy mode active since 4/11/2004 - 14:45:51 PM
[02:07:47] Gameplay in progress
[02:07:47]      Map : C&C_Canyon.mix
[02:07:47]     Time : 0.28.23
[02:07:47]      Fps : 60
[02:07:47]      GDI : 1/32 players      1059 points
[02:07:47]      NOD : 2/32 players      2618 points

i need this to be done into the following

Private type game
map As String
Time as String
fps as integer
gdip As Integer    (number of player on GDI)
gdiof As Integer   (maximum number of plauers on gdi)
nodp As Integer    (number of players on nod)
nodof As Integer   (maximum number of players on nod)
gamer As Boolean (True if it comes out as [02:07:47] Gameplay in progress False if anything else
gdipo As Integer (number of points team gdi has)
nodpo As Intger (number of points team nod has)
runs As String (how long its been running as in 1w 1d 2h etc can be found out from the line [02:07:47] GameSpy mode active since 4/11/2004 - 14:45:51 PM )
End Type

once again this comes out this comes in different strings with the For next loop

and one final thing i need it to ignore blank lines and if something comes up that does not go with any of the above i need it to be put into the following


Thanks advanced - Nightma12
Who is Participating?
TeychoConnect With a Mentor Commented:
The easiest way to do this is to use the Split() function.

It takes a string as an argument, and a delimiter on which to split, and returns an array of strings. In this case, you'd be using " " ( space ) as a delimiter.

str = "NOD Game Server Only"
arr = Split(str, " ")

Afterwards, arr =
arr(0) = "NOD"
arr(1) = "Game"
arr(2) = "Server"
arr(3) = "Only"

From there, it's easy to add th values into your UDT.  Of course, you'd need a slightly different routine for each different format in which the data comes back.

Nightma12Author Commented:
ok ive had ago at doing this function and i kinda messed up lol i sux

is there any chance of you being able to supply the code to use this function to produce what i am trying to do and detect which one it is etc

Thanks :)
This would be an example how to split the three chat modes:

Private Sub Command1_Click()
Dim myString As String, mySplit() As String, cTime As String, cNick As String
Dim cChatMode As String, cFristW As String, cSecondW As String, cThirdW As String, cFourthW As String
Dim cModerator As String, c As Integer

'myString = "[02:10:13] Host: DrkAgnt is a game moderator."
'myString = "[00:37:41] [Page] Host: DrkAgnt is a game moderator."
myString = "[00:37:41] [Team] ooops21: OK"

'detect chat mode
If InStr(1, myString, "Page") > 0 Then
        cChatMode = "Page"
        c = 1
    ElseIf InStr(1, myString, "Team") > 0 Then
        cChatMode = "Team"
        c = 1
        cChatMode = "All"
        c = 0
End If

mySplit = Split(myString, " ", -1)

On Error Resume Next         'we need it in case the line is shorter then the split ubound
cTime = Mid(mySplit(0), 2, 8)
cNick = mySplit(1 + c)
cFristW = mySplit(2 + c)
cSecondW = mySplit(3 + c)
cThirdW = mySplit(4 + c)
cFourthW = mySplit(5 + c)
cModerator = mySplit(6 + c)
'cChatMode - you already have it from above
End Sub

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Nightma12Author Commented:
ok ty it works :)

there is just 2 problems

the first word does not get into the string

i also need it for everything after the 4th word

anybody know about the game_info & player_info bits?
Nightma12Author Commented:
ok i should be able to do player_info myself with the Split() function thxs! :)

just need game_info now :) (and a fixed v of Shaulis code lol)
Nightma12Author Commented:
Ok, i had a Loooooong look at the Split() function and finally fiqured it out!

Thanks Guys! :)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.