3

I try to find an easy way to concatenate strings from an array - but only if they are not empty.

Example:

Dim strArr(3)
strArr(0) = "This"
strArr(1) = "is"
strArr(2) = ""
strArr(3) = "a"
strArr(4) = "test"

strResult = Join(strArr, "***")
MsgBox strResult

will result in this text: This***is******a***test

but it should be: This***is***a***test

So the empty string should be ignored.

3 Answers 3

4
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:

  1. 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.
  2. A regular expression is created to handle initial, end and repeated occurences of the delimiter.
  3. The input array is joined using the generated delimiter
  4. The regular expression is used to remove non desired delimiter sequences
  5. The generated delimiter is replaced by the input delimiter
Sign up to request clarification or add additional context in comments.

1 Comment

Seems that there is no built-in-way to handle this. Will use that function since it avoids problems if the delimiter is part of the text.
1

Next code snippet could help (and even ReDims the strArr):

option explicit
Dim strArr, strResult, strJoinString
ReDim strArr(4)
strArr(0) = "This"
strArr(1) = "is"
strArr(2) = ""
strArr(3) = "a"
strArr(4) = "test"

strJoinString = "€€€"
strResult = Join(strArr, strJoinString)
Do While Instr(1, strResult, strJoinString & strJoinString, vbBinaryCompare) > 0
  strResult = Replace( strResult, strJoinString & strJoinString, strJoinString)
Loop
strArr = Split(strResult, strJoinString)
strResult = Join(strArr, "***")

MsgBox strResult & vbNewLine & Join(strArr, "-")

Make sure strJoinString does not match any array element!

Comments

0

One way is to copy all non-empty fields to a dictionary, then join the values of that dictionary:

Set d = CreateObject("Scripting.Dictionary")

For i = 0 To UBound(strArr)
  If strArr(i) <> "" Then d.Add i, strArr(i)
Next

WScript.Echo Join(d.Items, "***")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.