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


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 = iChar
End Function
NOTE: The ReDim statement "Declares dynamic-array variables, and allocates or reallocates storage space at procedure level".

Example: c:\folder\test.vbs contains:
Dim inString, outAtrray, iPos, iLen, objArguments
Set objArguments = Wscript.Arguments
If WScript.Arguments.Count = 0 then
   Wscript.Echo "String Argument required."
   Wscript.Quit
End If
inString = objArguments(0)
outArray = splitOne(inString)
for iPos = 1 To Len(inString)
      WScript.Echo outArray(iPos - 1)
Next

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 = iChar
End Function

....................................................

cscript //nologo c:\folder\test.vbs "0123456789ABCD"

0
1
2
3
4
5
6
7
8
9
A
B
C
D



Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish