Tuesday, June 13, 2006

Parsing Strings

Available in my UW.DOT and UX.XLS libraries.
Nothing complex about it. We are often asked to split or parse strings into sub-strings.
Typically we receive a series of words separated by spaces or the comma character; sometimes separated by the tab character (vbTab in VBA)
We can peel off consecutive substrings in this manner:
Sub ParseStrings()
Dim strMyString As String
strMyString = "Nothing complex about it."
While Len(strMyString) > 0
MsgBox UW.strSplitAt(strMyString, " ")
Wend
End Sub
Of course, we need to know in advance what character to use as a delimiter.
Smart programming suggests that we adopt a convention of building a string of substrings by placing the delimiter as the first or left-most character:
strMyString = " Nothing complex about it."
Now we can write a parsing routine that is independent of the delimiter:
Sub ParseStrings()
Dim strMyString As String
strMyString = " Nothing complex about it."
Dim strDelimiter As String
strDelimiter = Left$(strMyString, 1)
While Len(strMyString) > 0
MsgBox UW.strSplitAt(strMyString, strDelimiter)
Wend
End Sub


Read more at http://www.chrisgreaves.com/BlogUtils/ParsingStrings.html)

0 Comments:

Post a Comment

<< Home