Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / VBScript
Article

Tokenizing strings in VBScript

Rate me:
Please Sign up or sign in to vote.
4.17/5 (6 votes)
16 May 2000CPOL 166.3K   20   11
A simple function that allows you to tokenize a string sing multiple token separators

This is an extremely simple function for tokenizing strings. You supply the function with a string that you wish to tokenize, and an array of tokens the delimit the tokens.

For example, suppose you have the string "Tom, Dick and Harry" and you'd like to break it up into "Tom", "Dick", "Harry". Your string is thus "Tom, Dick and Harry" and your array contains the "," and "and" separators:

VBScript
Dim Str, Seps(2)
Str     = "Tom, Dick and Harry"
Seps(0) = ","
Seps(1) = "and"

Dim i, a
a = Tokenize(Str, Seps)

Response.Write "<p>Found " & UBound(a) & " tokens</p>"
Response.Write "<ol>"
For i=1 to UBound(a)
	Response.Write "<li>Keyword " & i & " = " & a(i-1) & "</li>"
next
Response.Write "</ol>"

The results will be

Found 3 tokens
  1. Keyword 1 = Tom</tt>
  2. Keyword 2 = Dick</tt>
  3. Keyword 3 = Harry</tt>

The function is as follows:

VBScript
Function Tokenize(byVal TokenString, byRef TokenSeparators())

	Dim NumWords, a()
	NumWords = 0
	
	Dim NumSeps
	NumSeps = UBound(TokenSeparators)
	
	Do 
		Dim SepIndex, SepPosition
		SepPosition = 0
		SepIndex    = -1
		
		for i = 0 to NumSeps-1
		
			' Find location of separator in the string
			Dim pos
			pos = InStr(TokenString, TokenSeparators(i))
			
			' Is the separator present, and is it closest to the beginning of the string?
			If pos > 0 and ( (SepPosition = 0) or (pos < SepPosition) ) Then
				SepPosition = pos
				SepIndex    = i
			End If
			
		Next

		' Did we find any separators?	
		If SepIndex < 0 Then

			' None found - so the token is the remaining string
			redim preserve a(NumWords+1)
			a(NumWords) = TokenString
			
		Else

			' Found a token - pull out the substring		
			Dim substr
			substr = Trim(Left(TokenString, SepPosition-1))
	
			' Add the token to the list
			redim preserve a(NumWords+1)
			a(NumWords) = substr
		
			' Cutoff the token we just found
			Dim TrimPosition
			TrimPosition = SepPosition+Len(TokenSeparators(SepIndex))
			TokenString = Trim(Mid(TokenString, TrimPosition))
						
		End If	
		
		NumWords = NumWords + 1
	loop while (SepIndex >= 0)
	
	Tokenize = a
	
End Function

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder CodeProject
Canada Canada
Chris Maunder is the co-founder of CodeProject and ContentLab.com, and has been a prominent figure in the software development community for nearly 30 years. Hailing from Australia, Chris has a background in Mathematics, Astrophysics, Environmental Engineering and Defence Research. His programming endeavours span everything from FORTRAN on Super Computers, C++/MFC on Windows, through to to high-load .NET web applications and Python AI applications on everything from macOS to a Raspberry Pi. Chris is a full-stack developer who is as comfortable with SQL as he is with CSS.

In the late 1990s, he and his business partner David Cunningham recognized the need for a platform that would facilitate knowledge-sharing among developers, leading to the establishment of CodeProject.com in 1999. Chris's expertise in programming and his passion for fostering a collaborative environment have played a pivotal role in the success of CodeProject.com. Over the years, the website has grown into a vibrant community where programmers worldwide can connect, exchange ideas, and find solutions to coding challenges. Chris is a prolific contributor to the developer community through his articles and tutorials, and his latest passion project, CodeProject.AI.

In addition to his work with CodeProject.com, Chris co-founded ContentLab and DeveloperMedia, two projects focussed on helping companies make their Software Projects a success. Chris's roles included Product Development, Content Creation, Client Satisfaction and Systems Automation.

Comments and Discussions

 
GeneralNested token's Pin
deadtroll13-Oct-06 9:17
deadtroll13-Oct-06 9:17 
GeneralNested Tokenize not working VBScript/ASP Pin
Ditta17-Jul-03 5:54
Ditta17-Jul-03 5:54 
GeneralYet another version Pin
slash17-May-00 3:55
slash17-May-00 3:55 
GeneralRe: Yet another version Pin
Anonymous9-Mar-05 13:06
Anonymous9-Mar-05 13:06 
QuestionSo you extended the VBScript Split function? Pin
Uwe Keim17-May-00 1:08
sitebuilderUwe Keim17-May-00 1:08 
AnswerRe: So you extended the VBScript Split function? Pin
Chris Maunder17-May-00 12:47
cofounderChris Maunder17-May-00 12:47 
GeneralRe: So you extended the VBScript Split function? Pin
6-Jul-01 7:55
suss6-Jul-01 7:55 
GeneralRe: So you extended the VBScript Split function? Pin
19-Jul-01 1:51
suss19-Jul-01 1:51 
GeneralRe: So you extended the VBScript Split function? Pin
Gareth Watson10-Jul-02 0:10
Gareth Watson10-Jul-02 0:10 
AnswerRe: So you extended the VBScript Split function? Pin
DDarling11-Jul-02 9:26
DDarling11-Jul-02 9:26 
GeneralRe: So you extended the VBScript Split function? Pin
Anonymous17-Nov-03 5:21
Anonymous17-Nov-03 5:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.