VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



A 'Parse' function.

by Danny Young (3 Submissions)
Category: String Manipulation
Compatability: Visual Basic 3.0
Difficulty: Intermediate
Date Added: Wed 3rd February 2021
Rating: (18 Votes)

To split a string into pieces using a certain character as a delimiter. I do not want to get messages saying, "use the Split() function" as this isn't present in VB5.
Example of this is "hello to you", with the delimiter as " ". You'll get back 3 variables, one containing "hello", one containing "to" and one containing "you"

Inputs
The string thats going to be split and the delimiter in which to split it with.
Assumes
Put a button on a form, and leave it as the default name
Code Returns
An array of parsed words from the string

Rate A 'Parse' function.

Option Explicit
Private Sub Command1_Click()
 Dim A As Variant
 Dim i As Integer
 i = 1
 A = Parse("hello to you", " ")
 Do While A(i) <> ""
 MsgBox A(i)
 i = i + 1
 Loop
End Sub
Public Function Parse(sIn As String, sDel As String) As Variant
 Dim i As Integer, x As Integer, s As Integer, t As Integer
 i = 1: s = 1: t = 1: x = 1
 ReDim tArr(1 To x) As Variant
 If InStr(1, sIn, sDel) <> 0 Then
  Do
   ReDim Preserve tArr(1 To x) As Variant
   tArr(i) = Mid(sIn, t, InStr(s, sIn, sDel) - t)
   t = InStr(s, sIn, sDel) + Len(sDel)
   s = t
   If tArr(i) <> "" Then i = i + 1
   x = x + 1
  Loop Until InStr(s, sIn, sDel) = 0
  ReDim Preserve tArr(1 To x) As Variant
  tArr(i) = Mid(sIn, t, Len(sIn) - t + 1)
 Else
  tArr(1) = sIn
 End If
 Parse = tArr
End Function

Download this snippet    Add to My Saved Code

A 'Parse' function. Comments

No comments have been posted about A 'Parse' function.. Why not be the first to post a comment about A 'Parse' function..

Post your comment

Subject:
Message:
0/1000 characters