CorString: Join

Join

Concatenates the specified elements of an array or collection, using the specified separator between each element.



 Public Function Join(
	  ByRef Separator As String,
	  ByRef Value As Variant,
	  Optional ByRef StartIndex As Variant,
	  Optional ByRef Count As Variant ) As String

Parameters

Separator
[ByRef] String. The string to use as a saparator.
Value
[ByRef] Variant. An array or collection that contains the elements to concatenate.
StartIndex
[ByRef] Optional. Variant. The first element in Value to use.
Count
[ByRef] Optional. Variant. The number of elements of Value to use.

Return Values

String -  A string that consists of the strings in value delimited by the separator string.
-or-
"" if Count is zero, Value has no elements, or Separator and all the elements of Value are "".

Remarks

For example, if Separator is ", " and the elements of Value are "apple", "orange", "grape", and "pear", Join(separator, value, 1, 2) returns "orange, grape".

Exceptions

Exception Condition
ArgumentOutOfRangeException StartIndex is less than the lower-bound of Value.
-or-
Count is less than zero.
-or-
StartIndex plus Count is greater than the upper-bound of Value.

Examples

The following example concatenates two elements from an array of names of fruit.

Public Sub Main()
    Dim Value() As String
    Dim List    As Collection
    Dim Sep     As String
    Dim Result  As String
    
    Sep = ", "
    Value = NewStrings("apple", "orange", "grape", "pear")
    Set List = NewCollection("apple", "orange", "grape", "pear")
    
    Debug.Print CorString.Format("Sep = '{0}'", Sep)
    Debug.Print
    
    Debug.Print CorString.Format("Value() = {{'{0}' '{1}' '{2}' '{3}'}}", Value(0), Value(1), Value(2), Value(3))
    Result = CorString.Join(Sep, Value, 1, 2)
    Debug.Print CorString.Format("CorString.Join(Sep, Value, 1, 2) = '{0}'", Result)
    Debug.Print
    
    Debug.Print CorString.Format("List = {{'{0}' '{1}' '{2}' '{3}'}}", List(1), List(2), List(3), List(4))
    Result = CorString.Join(Sep, List)
    Debug.Print CorString.Format("CorString.Join(Sep, List) = '{0}'", Result)
End Sub

' This example produces the following output.
'
'    Sep = ', '
'
'    Value() = {'apple' 'orange' 'grape' 'pear'}
'    CorString.Join(Sep, Value, 1, 2) = 'orange, grape'
'
'    List = {'apple' 'orange' 'grape' 'pear'}
'    CorString.Join(Sep, List) = 'apple, orange, grape, pear'

See Also

Project CorLib Overview

Class CorString Overview

Split