VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Splitting Strings

by Particle (1 Submission)
Category: String Manipulation
Compatability: Visual Basic 3.0
Difficulty: Beginner
Date Added: Wed 3rd February 2021
Rating: (3 Votes)

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.

Rate Splitting Strings

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.

Download this snippet    Add to My Saved Code

Splitting Strings Comments

No comments have been posted about Splitting Strings. Why not be the first to post a comment about Splitting Strings.

Post your comment

Subject:
Message:
0/1000 characters