Skip to content

Core\Str::join

Joins a list of strings into one string.

Core\Str::join takes an array of strings and a separator. It returns one string that has the separator between each two neighbouring strings, in the order of the array. The separator is not added before the first string or after the last one. If you leave out the separator, the strings are joined with nothing between them.

An empty array gives the empty string "". An array with one string gives that string. An empty string in the array still gets a separator on each side, so ["a", "", "b"] joined with "," gives "a,,b".

Core\Str::split does the opposite: it cuts one string into a list at a separator.

Good to know: the array always comes first, and the separator second.

Parameters

  • $partsarray<string>The strings to join, in the order of the array.
  • $separatorstringdefault ""The text that goes between two neighbouring strings. The default is the empty string "".

Return value

Returns string. The joined string. An empty array gives "".

Examples

A list of names

<?nvs
// Shows how to join a list of names into one line of text.
array<string> $names = ['Anna', 'Ben', 'Carla'];
// `Core\Str::join` puts ", " between each two names.
// This prints `Anna, Ben, Carla`.
echo Core\Str::join($names, ', '), "\n";
// A list with one name has no neighbours, so no separator is added.
// This prints `Anna`.
array<string> $one = ['Anna'];
echo Core\Str::join($one, ', '), "\n";
Output
Anna, Ben, Carla
Anna

Without a separator

<?nvs
// Shows how to join pieces with nothing between them.
// Without a separator, the pieces are joined directly.
// This prints `report-20260928.csv`.
array<string> $date = ['2026', '09', '28'];
echo 'report-', Core\Str::join($date), ".csv\n";
// An empty list gives the empty string `""`.
// This prints `[]`.
array<string> $nothing = [];
echo '[', Core\Str::join($nothing), "]\n";
// An empty string in the list still gets a separator on each side.
// This prints `a,,b`.
array<string> $gap = ['a', '', 'b'];
echo Core\Str::join($gap, ','), "\n";
Output
report-20260928.csv
[]
a,,b

Save tags as one line

<?nvs
// Shows how to clean the tags a user typed into a form and save them as one line of text.
array<string> $typed = [' coffee', 'tea ', '', ' ', 'cocoa'];
// `Core\Str::trim` removes the spaces around each tag.
// An empty tag is skipped, so the line never has two commas in a row.
array<string> $tags = [];
foreach ($typed as string $tag) {
string $clean = Core\Str::trim($tag);
if (!Core\Str::isEmpty($clean)) {
$tags[] = $clean;
}
}
// This prints `coffee,tea,cocoa`.
echo Core\Str::join($tags, ','), "\n";
Output
coffee,tea,cocoa