strcat including space (i.e, ' ')
806 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
R P
el 11 de Jun. de 2011
Respondida: Jy·Li
el 25 de Mayo de 2023
I have to concatenate words, including spaces
Ex. a='word1'; b='word2';c=strcat(a,' ',b);
I need 'word1 word2', however, the value on c is 'word1word2'
Can you help me?
0 comentarios
Respuesta aceptada
Walter Roberson
el 11 de Jun. de 2011
Editada: MathWorks Support Team
el 8 de Nov. de 2018
To include spaces when concatenating character vectors, use square brackets.
a = 'word1';
b = 'word2';
c = [a ' ' b]
The “ strcat ” function ignores trailing whitespace characters in character vectors. However, “strcat” preserves them in cell arrays of character vectors or string arrays.
a = {'word1'};
b = {'word2'};
c = strcat(a,{' '},b)
You also can use the “plus” operator to combine strings. Starting in R2017a, use double quotes to create strings. For more information on strings, see the “ string ” data type.
a = "word1";
b = "word2";
c = a + " " + b
6 comentarios
Captain Karnage
el 26 de Ag. de 2022
Editada: Voss
el 26 de Ag. de 2022
FYI for anyone else reading, I had a cell array of strings and I wanted to concatenate the same string (with a space in it) to each of the strings in the cell array (no spaces in these strings). The square bracket (first) method of course doesn't work, neither does the "plus" operator (third/last) method for that. However, the "strcat" using a single cell array with a space (2nd/middle above) method does work.
To specifically show an example:
a = 'Addme';
b = { 'to', 'each', 'one', 'of', 'these', 'words', 'with', 'a', 'space'};
c = strcat(a,{' '},b)
Walter Roberson
el 26 de Ag. de 2022
Editada: Walter Roberson
el 26 de Ag. de 2022
a = 'Addme';
b = { 'to', 'each', 'one', 'of', 'these', 'words', 'with', 'a', 'space'};
strjoin([a, b])
a + " " + b
Más respuestas (4)
Paulo Silva
el 11 de Jun. de 2011
c=[a ' ' b]
strcat ignores trailing ASCII white space characters and omits all such characters from the output. White space characters in ASCII are space, newline, carriage return, tab, vertical tab, or form-feed characters, all of which return a true response from the MATLAB isspace function. Use the concatenation syntax [s1 s2 s3 ...] to preserve trailing spaces. strcat does not ignore inputs that are cell arrays of strings.
2 comentarios
Daniel Foose
el 23 de Feb. de 2018
This is better than the accepted answer because it keeps the type the same. The accepted answer returns a cell with a string in it (which is different from a string). This answer returns a string.
Walter Roberson
el 23 de Feb. de 2018
The accepted answer returns a cell with a character vector in it. Strings did not exist in R2011a. If strings were being used then you would use a different approach:
>> a = "word1"; b = "word2"; a + " " + b
ans =
"word1 word2"
This requires R2017a or later. For R2016b,
>> a = string('word1'); b = string('word2'); a + ' ' + b
and before R2016b strings did not exist.
Usman Nawaz
el 6 de Sept. de 2020
use double quotes instead of single quotes, worked for me.
1 comentario
Walter Roberson
el 6 de Sept. de 2020
That can be useful, but the output would be a string() object instead of a character vector. string() objects can be useful, but they need slightly different handling than character vectors.
string() objects became available in R2016b; using double-quotes to indicate string objects became available in R2017a.
R P
el 11 de Jun. de 2011
3 comentarios
Walter Roberson
el 11 de Jun. de 2011
>> strcat({'word1'},{' '},{'word2'})
ans =
'word1 word2'
You can dereference this or cell2mat it if you want the string itself as output.
Ver también
Categorías
Más información sobre Characters and Strings en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!