Join method: Array class

Syntax

Join([separator [, arraystart, arrayend  [,stringsizehint]]])

Description

The Join method converts the array that is executing the method into a string by converting each element into a string and joining these strings together, separated by separator.

Note:

Join does not join two arrays together.

Each array or subarray to be joined is preceded by the string given by arraystart and followed by the string given by arrayend. If the given array is multi-dimensional, then (logically) each subarray is first joined, then the resulting strings are joined together.

Parameters

Parameter Description

separator

Specifies what the elements in the resulting string should be separated with in the resulting string. Separator is set by default to a comma (",").

arraystart

Specifies what each array or subarray to be joined should be preceded with in the resulting string. arraystart is set by default to a left parenthesis ("(").

arrayend

Specifies what each array or subarray to be joined should be followed by in the resulting string. arrayend is set by default to a right parenthesis (")").

stringsizehint

Specify a hint to the Join method about the resulting size of the string. This can improve performance if your application is concatenating a large number of string. See the Example section below.

Returns

A string containing the converted elements of the array.

Example

The following example:

Local array of array of number &AAN;

&AAN = CreateArray(CreateArray(1, 2), CreateArray(3, 4), 5);
&STR = &AAN.Join(", ");

produces in &STR the string:

((1, 2), (3, 4), 5)

The following example makes use of the stringsizehint parameter. The following application class passes the resulting string size hint in the Value property.

class StringBuffer
   method StringBuffer(&InitialValue As string, &MaxSize As integer);
   method Append(&New As string);
   method Reset();
   property string Value get set;
   property integer Length readonly;
private
   instance array of string &Pieces;
   instance integer &MaxLength;
end-class;

method StringBuffer
   /+ &InitialValue as String, +/
   /+ &MaxSize as Integer +/
   &Pieces = CreateArray(&InitialValue);
   &MaxLength = &MaxSize;
   &Length = 0;
end-method;

method Reset
   &Pieces = CreateArrayRept("", 0);
   &Length = 0;
end-method;

method Append
   /+ &New as String +/
   Local integer &TempLength = &Length + Len(&New);
   If &Length > &MaxLength Then
      throw CreateException(0, 0, "Maximum size of StringBuffer exceeded(" | &Max⇒
Length | ")");
   End-If;
   &Length = &TempLength;
   &Pieces.Push(&New);
end-method;

get Value
   /+ Returns String +/
   Local string &Temp = &Pieces.Join("", "", "", &Length);
   /* collapse array now */
   &Pieces = CreateArrayRept("", 0);
   &Pieces.Push(&Temp); /* start out with this combo string */
   Return &Temp;
end-get;

set Value
   /+ &NewValue as String +/
   /* Ditch our current value */
   %This.Reset();
   &Pieces.Push(&NewValue);
end-set;
 

The following code concatenates strings.

While &file.ReadLine(&line)
   &S.Append(&line);
   &S.Append(&separator);