Main Content

join

Combine strings

Description

example

newStr = join(str) combines the text in str by joining consecutive elements of the input array, placing a space character between them. str can be a string array or a cell array of character vectors. newStr has the same data type as str.

  • If str is a 1-by-N or an N-by-1 string array or cell array, then newStr is a string scalar or a cell array that contains one character vector.

  • If str is an M-by-N string array or cell array, then newStr is an M-by-1 string array or cell array.

For a string or cell array of any size, join concatenates elements along the last dimension of str with a size that does not equal 1.

example

newStr = join(str,delimiter) combines the text in str and places the elements of delimiter between the elements of str instead of a space character.

If the delimiter argument is an array of different delimiters, and str has N elements along the dimension that is joined, then delimiter must have N–1 elements along the same dimension. The other dimensions of delimiter must have either a size of 1 or the same size as the corresponding dimension of str.

example

newStr = join(str,dim) combines the elements in str along the dimension dim.

newStr = join(str,delimiter,dim) combines the elements in str along the dimension dim and places the elements of delimiter between the elements of str.

Examples

collapse all

Create a string array. You can create strings using double quotes.

str = ["Carlos","Sada";
       "Ella","Olsen";
       "Diana","Lee"]
str = 3x2 string
    "Carlos"    "Sada" 
    "Ella"      "Olsen"
    "Diana"     "Lee"  

Combine the strings using the join function. join concatenates the strings from str and places a space character between them. join concatenates along the second dimension, because it is the last dimension with a size that does not equal 1.

newStr = join(str)
newStr = 3x1 string
    "Carlos Sada"
    "Ella Olsen"
    "Diana Lee"

Combine elements in a string array. Instead of spaces, insert different pieces of text between the strings in str.

Create a string array.

str = ["x","y","z";
       "a","b","c"]
str = 2x3 string
    "x"    "y"    "z"
    "a"    "b"    "c"

Concatenate the strings with dashes between them.

newStr = join(str,"-")
newStr = 2x1 string
    "x-y-z"
    "a-b-c"

Concatenate the strings with symbols that make the output strings represent equations. The delimiters argument must be a 2-by-2 array because str is a 2-by-3 array.

delimiters = [" + "," = ";
              " - "," = "];
newStr = join(str,delimiters)
newStr = 2x1 string
    "x + y = z"
    "a - b = c"

Create a string array.

str = ["Carlos","Sada";
       "Ella","Olsen";
       "Diana","Lee"]
str = 3x2 string
    "Carlos"    "Sada" 
    "Ella"      "Olsen"
    "Diana"     "Lee"  

Combine the strings in str along the first dimension. By default, the join function combines strings along the last dimension with a size that does not equal 1. To combine the strings along the first dimension, specify it as an additional input argument.

newStr = join(str,1)
newStr = 1x2 string
    "Carlos Ella Diana"    "Sada Olsen Lee"

Input Arguments

collapse all

Input text, specified as a string array or a cell array of character vectors.

Delimiting characters for joining strings, specified as a character vector, a cell array of character vectors, or a string array. join forms the output string array by combining string elements with delimiters between them.

join inserts all characters in delimiter as literal text, including escaped character sequences.

Dimension along which to join strings, specified as a positive integer. If dim is not specified, then the default is the last dimension with a size that does not equal 1.

Output Arguments

collapse all

Output text, returned as a string array or a cell array of character vectors. newStr has the same data type as the input text and has a size of 1 along the dimension being joined.

Extended Capabilities

Version History

Introduced in R2016b