Option Explicit
Dim aStrArray
aStrArray = Array("", "", "This", "", "", "is", "", "", "", "a", "test", "", "")
Dim strResult
strResult = MyJoin(aStrArray, "***")
WScript.Echo strResult
Function MyJoin( inputArray, inputDelimiter )
Dim delimiter
delimiter = Chr(1) & Chr(2)
With New RegExp
.Pattern = "^(?:" & delimiter & ")+" & _
"|(?:" & delimiter & ")+$" & _
"|(?:" & delimiter & ")+(" & delimiter & ")"
.Global = True
MyJoin = Replace(.Replace(Join(inputArray, delimiter), "$1"), delimiter, inputDelimiter)
End With
End Function
You can replace multiple occurrences of the delimiter with only one.
In the function in sample code:
- A delimiter is defined to be used to join all the elements in the input array. It is created to reduce the probability of finding it inside the data.
- A regular expression is created to handle initial, end and repeated occurences of the delimiter.
- The input array is joined using the generated delimiter
- The regular expression is used to remove non desired delimiter sequences
- The generated delimiter is replaced by the input delimiter