Join - VBScript


Join

Overview:

The Join method combines two or more strings into a single string, separating them with a specified delimiter. It is commonly used to create a comma-separated list, concatenate strings dynamically, or format text output.

Syntax:

Join(sourceArray, delimiter)

Parameters:

  • sourceArray: A one-dimensional array of strings to be joined.
  • delimiter: Optional. The string to be inserted between each array element in the resulting string. The default delimiter is an empty string (no separation).

Options/Flags:

None

Examples:

Simple join:

Dim strArray = {"a", "b", "c"}
MsgBox(Join(strArray, ", ")) ' Output: "a, b, c"

Custom delimiter:

MsgBox(Join(strArray, " - ")) ' Output: "a - b - c"

Dynamic concatenation:

Dim date = Date
Dim time = Time
MsgBox(Join({date, time}, " - ")) ' Output: "2023-03-08 - 14:35:12"

Common Issues:

  • Ensure that the source array contains only strings. Mixing data types can lead to unexpected results.
  • If the delimiter is not present, all array elements will be concatenated without separation.

Integration:

The Join method can be combined with other VBScript functions and commands to build more complex scripts. For example:

' Get a list of files in a directory
Dim files = Split(FileSystemObject.GetFolder("c:\").Files, vbCrLf)
' Join the files into a single comma-separated string
Dim joinedFiles = Join(files, ", ")
' Save the list to a text file
FileSystemObject.OpenTextFile("c:\fileList.txt", vbUnicode).WriteLine(joinedFiles)
  • Split: Divides a string into an array of substrings.
  • Replace: Replaces occurrences of a substring with another specified string.
  • vbCrLf: Represents the line break character sequence in VBScript.