Split

Parse a string of delimited values into an array.

Syntax 
      Split(expression [,Delimiter [,count [,compare]]] )

Key
   expression  The string expression to be broken up into an array.

   Delimiter   The character used to separate items (default=a space " ")

   count       The number of strings

   compare    vbBinaryCompare (0) or vbTextCompare(1)

Examples

Create an array from a comma separated list of prices:

arrPrices = Split("23.50, 67.50, 45.99, 18.47", ",")
WScript.Echo arrPrices(0)
> 23.50

Script to open the file quotes.txt and split it on each line ending, returning one quote of the day:

'
' run this with:
'   cscript fortune.vbs quotes.txt > quote-of-the-day.txt //nologo
'
Set objFS = CreateObject("Scripting.FileSystemObject")
Set strFileContent = objFS.OpenTextFile(WScript.Arguments(0))

arrQuotes = Split(strFileContent.ReadAll, vbCrLf)

Randomize Timer
WScript.Echo arrQuotes(Round(Rnd * UBound(arrQuotes)))

“A gentle stream can split a mountain, given enough time”

Related VBScript commands

Array - Add values to an Array variable.
Join - Combine the contents of an array into a single variable.
Equivalent in PowerShell: .Split method.


 
Copyright © 1999-2024 SS64.com
Some rights reserved