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

Posted on 2004-04-21
Medium Priority
Last Modified: 2010-05-02

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
Question by:Nightma12
  • 4

Accepted Solution

Teycho earned 615 total points
ID: 10881361
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.


Author Comment

ID: 10881645
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 :)
LVL 19

Expert Comment

ID: 10881984
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 10891035
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?

Author Comment

ID: 10904586
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)

Author Comment

ID: 11017388
Ok, i had a Loooooong look at the Split() function and finally fiqured it out!

Thanks Guys! :)

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question