README.md

December 4, 2022 · View on GitHub

Splitting Strings

Description

The purpose of this article is to teach the reader how to split a string into fragments and use the fragments effectively. Useful for Winsock programs, date commands, and other possible needs.

More Info

Submitted On
ByParticle
LevelBeginner
User Rating5.0 (15 globes from 3 users)
CompatibilityVB 6.0
CategoryString Manipulation
WorldVisual Basic
Archive File

Source Code

In order to split a string, you'll need a source. An internal string or a textbox (and so on; any major text source) will do fine. In addition to the source, you'll need something to use it. Look at the code below for a the raw code:

output = Split(source, split object)(part number)

If you wanted to split a field called Input into two sections, then you'd do the following:

Private Sub Command1_Click()
    Dim InputString as String
    Dim Output1 as String
    Dim Output2 as String

    'Sets the input field to a sample string that could be used in a chat program for example
    InputString = "Particle:::Hello bob!"

    Output1 = Split(InputString, ":::")(0)
    Output2 = Split(InputString, ":::")(1)
Exit Sub

Alright, do you see how it works? You basically identify what you want split, what sequence to split it at, and which segment to set that particular output string to use. You could go on further with the segment number for if you had something like this perhaps: "Particle:::How's it goin?:::Yo".

That code would yield the following:

Output1 = "Particle"
Output2 = "Hello bob!"

Notice that the splitting sequence is removed. Add the following code before the rest if your string may not always have the same number of segments:

On Error Resume Next

FINAL SAMPLE:
Private Sub Command1_Click()
    Dim InputString as String
    Dim Output1 as String
    Dim Output2 as String
    Dim Output3 as String

    'Sets the input field to a sample string that could be used in a chat program for example
    InputString = "Particle:::Hello bob!:::Yo"

    Output1 = Split(InputString, ":::")(0)
    Output2 = Split(InputString, ":::")(1)
    Output3 = Split(InputString, ":::")(2)
Exit Sub

You don't have to use every segment either. You could use segments 0, 4, and 11 for example.

This is also very handy for splitting the Time command, identifying things for winsock applications etc. Use it in the same way, splitting on the ":"s for example.