|
|
Joined: Dec 1999
Posts: 30,867
Master Elite Member
|
Public Function Str_ParseToSArr(ByVal MyString As String, Delimiter As String, AutoTrim As Boolean) As String()
'Purpose
' Used to parse elements in a string to a string array.
' The elements in the string are delimited by the specified delimiter.
'Parameters
' MyString (String) = The string we're parsing.
' Delimiter (String) = The first character in this string is used as the delimiter.
' If more than one characters exists, only the first character is used.
' AutoTrim (Boolean) = If True, all whitespace is trimmed in the returned array.
' If False, whitespace is honored and not removed at all.
'Return (String())
' Returns an array of String where each element contains an element from MyString.
' The elements are returned in the same order as they appear in MyString.
' All leading and trailing whitespace is removed from each element.
' Returns an empty array if:
' The specified string (MyString) contains no elements
' The specified delimiter (Delimiter) is zero-length
'Version History
' 1.0 3/30/2006 5:46am Tested by Russ Tanner on VB6 Win2000
'---
'Initiate variables
Dim X As String 'the current element in the loop
Dim D As String 'the normalized delimiter
Dim R() As String 'return buffer
'Trap: The specified delimiter is zero-length
If Len(Delimiter) = 0 Then
Str_ParseToSArr = R
Exit Function
End If
'Normalize the delimiter
D = Left(Delimiter, 1)
'Main parse loop
Do While True
'AutoTrim is enabled. We must remove leading/trailing whitespace before processing.
If AutoTrim Then MyString = Trim(MyString)
'Trap: No elements left to process, exit the loop now
If Len(MyString) = 0 Then Exit Do
'Get the next element and cut MyString...
'MyString contains a delimiter, use it to get the next element
If InStr(MyString, D) > 0 Then
X = Left(MyString, InStr(MyString, D) - 1)
MyString = Right(MyString, Len(MyString) - InStr(MyString, D))
'MyString does not have a delimiter, just get the whole rest of MyString
Else
X = MyString
MyString = ""
End If
'AutoTrim is enabled; Automatically trim each element
If AutoTrim Then X = Trim(X)
'Append a new element to the return buffer
ReDim Preserve R(ElementCount(R))
R(UBound(R)) = X
Loop
'Return the result
Str_ParseToSArr = R
End Function
|
|
|
|
Entire Thread
|
Used to parse elements in a string to a string array.
|
Russ
|
08/11/08 10:32 PM
|
|
|
|