strjoin

Joins the pieces of a cell array of text into one text with a delimiter.

Category: Language Fundamentals > Data Types

Syntax

s = strjoin(C)

Joins the elements of C separated by a single space.

s = strjoin(C, delimiter)

Uses delimiter between the elements.

Description

strjoin is the inverse of strsplit: it builds a comma-separated list, a path, or a multi-line text from a cell array. A one-element cell returns that element, and an empty cell returns an empty text.

All elements must be text, so convert numbers first, for example with strjoin(string(v), ', ') or strjoin(cellfun(@num2str, num2cell(v), 'UniformOutput', false), ', ').

Input arguments

C
Cell array of character vectors (or string array).
delimiter
Text placed between elements, default ' '. Escape sequences such as '\n' and '\t' are interpreted.

Output arguments

s
One text with all the pieces joined: a character vector, or a string if the input is a string array.

Example

Different delimiters

strjoin({'a','b','c'}, ', ')
strjoin({'x','y'})
strjoin(string([1 2 3]), '+')
Output
ans = 'a, b, c'
ans = 'x y'
ans = "1+2+3"

See also

strsplit join strcat newline sprintf