JSI Tip 10450. How can a Vbscript Function return a zero-based, one-dimensional array of each character in a string?

Jerold Schulman

April 30, 2006

1 Min Read
ITPro Today logo in a gray background | ITPro Today


The Vbscript Split Function "Returns a zero-based, one-dimensional array containing a specified number of substrings", but it requires a delimiter.

I have scripted a SplitOne Function to return a zero-based, one-dimensional array of each character in a string:

Function splitOne( inStr )    Dim   iLen, iPos      iLen  = Len(inStr)    ReDim iChar(iLen - 1)    For iPos = 1 To iLen       iChar(iPos - 1) = Mid(inStr, iPos, 1)    Next    splitOne = iCharEnd Function

NOTE: The ReDim statement "Declares dynamic-array variables, and allocates or reallocates storage space at procedure level".

Example:c:foldertest.vbs contains:

Dim inString, outAtrray, iPos, iLen, objArgumentsSet objArguments = Wscript.ArgumentsIf WScript.Arguments.Count = 0 then   Wscript.Echo "String Argument required."   Wscript.QuitEnd IfinString = objArguments(0)outArray = splitOne(inString)for iPos = 1 To Len(inString)      WScript.Echo outArray(iPos - 1)NextFunction splitOne( inStr )    Dim   iLen, iPos      iLen  = Len(inStr)    ReDim iChar(iLen - 1)    For iPos = 1 To iLen       iChar(iPos - 1) = Mid(inStr, iPos, 1)    Next    splitOne = iCharEnd Function....................................................cscript //nologo c:foldertest.vbs "0123456789ABCD"0123456789ABCD



Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like