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

Posted on 2004-04-21
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 205 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.


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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Adding to a VBA? 6 70
Modifying Conditional Format from VBA code 3 57
Visual Studio 2005 text editor 10 39
MS Access 03, TransferText, decimal places 8 72
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…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

810 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