Combine strings
collapse all in page
Syntax
newStr = join(str)
newStr = join(str,delimiter)
newStr = join(str,dim)
newStr = join(str,delimiter,dim)
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 a1
-by-N
or anN
-by-1
string array or cell array, thennewStr
is a string scalar or a cell array that contains one character vector.If
str
is anM
-by-N
string array or cell array, thennewStr
is anM
-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
Combine Strings in String Array
Open Live Script
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 of String Array with Delimiters
Open Live Script
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"
Combine String Array Along Specified Dimension
Open Live Script
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
str
— Input text
string array | cell array of character vectors
Input text, specified as a string array or a cell array of character vectors.
delimiter
— Delimiting characters for joining strings
' '
(default) | character vector | cell array of character vectors | string array
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.
dim
— Dimension along which to join strings
positive integer
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
newStr
— Output text
string array | cell array of character vectors
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
Tall Arrays
Calculate with arrays that have more rows than fit in memory.
This function fully supports tall arrays. Formore information, see Tall Arrays.
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced in R2016b
See Also
split | regexp | string | splitlines | newline | compose | sprintf | plus
Comando de MATLAB
Ha hecho clic en un enlace que corresponde a este comando de MATLAB:
Ejecute el comando introduciéndolo en la ventana de comandos de MATLAB. Los navegadores web no admiten comandos de MATLAB.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom (English)
Contact your local office