Concatenate strings horizontally
collapse all in page
Syntax
s = strcat(s1,...,sN)
Description
example
Note
append is recommended over strcat
because it provides greater flexibility and allows vectorization. For additional information, see Alternative Functionality.
s = strcat(s1,...,sN)
horizontally concatenates the text in its input arguments. Each input argument can be a character array, a cell array of character vectors, or a string array.
If any input is a string array, then the result is a string array.
If any input is a cell array, and none are string arrays, then the result is a cell array of character vectors.
If all inputs are character arrays, then the result is a character array.
For character array inputs, strcat
removes trailing ASCII whitespace characters: space, tab, vertical tab, newline, carriage return, and form feed. For cell array and string array inputs, strcat
does not remove trailing white space.
For faster performance and to preserve trailing whitespace characters, use append.
Examples
collapse all
Concatenate Two Character Vectors
Create two character vectors. Use strcat
to horizontally concatenate the two vectors.
s1 = 'Good';s2 = 'morning';s = strcat(s1,s2)
s = 'Goodmorning'
Concatenate Two Cell Arrays
Open Live Script
Create two cell arrays of character vectors. Use strcat
to horizontally concatenate the elements of the two cell arrays.
s1 = {'abcde','fghi'};s2 = {'jkl','mn'};s = strcat(s1,s2)
s = 1x2 cell {'abcdejkl'} {'fghimn'}
Concatenate Two Cell Arrays with Scalar Cell Array
Open Live Script
Create two cell arrays of character vectors. Create a scalar cell array containing the character vector ','.
Use strcat
to horizontally concatenate the elements of the two cell arrays and the cell scalar.
firstnames = {'Abraham'; 'George'};lastnames = {'Lincoln'; 'Washington'};commas = {', '};names = strcat(lastnames, commas, firstnames)
names = 2x1 cell {'Lincoln, Abraham' } {'Washington, George'}
Concatenate Two String Arrays
Open Live Script
Concatenate text with the strcat
function. Note that when concatenated in this way the output string will insert a whitespace character between the input strings.
str1 = ["John ","Mary "];str2 = ["Smith","Jones"];str = strcat(str1,str2)
str = 1x2 string "John Smith" "Mary Jones"
Strings and character vectors can be combined using strcat
. When concatenating strings with character vectors a whitespace will not be added. Concatenate a character vector onto each element of the string array.
str3 = strcat(str,', M.D.')
str3 = 1x2 string "John Smith, M.D." "Mary Jones, M.D."
To combine strings and character vectors, consider using +
instead.
str4 = str + ', M.D.'
str4 = 1x2 string "John Smith, M.D." "Mary Jones, M.D."
Input Arguments
collapse all
s1,...,sN
— Input text
character arrays | cell array of character vectors | string arrays
Input text, specified as character arrays, cell arrays of character vectors, or string arrays. When combining string or cell arrays with character arrays, the string or cell arrays must be either scalars or column vectors with the same number of rows as the character arrays.
Data Types: char
| cell
| string
Alternative Functionality
Update existing code that makes use of strcat
to use append or syntaxes specific to character vectors and strings. Note that append
does not remove trailing whitespace characters. Character arrays also can be concatenated using left and right square brackets. String arrays can be concatenated using the +
operator. For example:
Not Recommended | Recommended | Square Brackets | + Operator |
---|---|---|---|
char1 = 'Good ';char2 = 'Morning';char3 = strcat(char1,char2) char3 = 'GoodMorning' | char1 = 'Good ';char2 = 'Morning';char3 = append(char1,char2) char3 = 'Good Morning' | char1 = 'Good ';char2 = 'Morning';char3 = [char1 char2] char3 = 'Good Morning' | str1 = "Good ";str2 = "Morning";str3 = str1 + str2 str3 = "Good Morning" |
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
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.
Version History
Introduced before R2006a
See Also
append | plus | cat | vertcat | horzcat | cellstr | strjoin | join
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
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