strcat including space (i.e, ' ')

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?

 采纳的回答

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 个评论

Walter I'm sorry but this time I have to disagree with your answer, you said strcat({'word1'},{' '},{'word2'}) but that doesn't work properly for the question and example, that's working with cells (input and output) and the user just wants strings.
So? The user also wanted variables instead of constants. I illustrated the principle and the user can adapt from there, such as using
c = char(strcat(a,{' '},b));
@CELL/STRCAT is a not efficiently implemented M-function. It is easy to improve it and remove the strange deleting of marinal spaces. This behaviour is kept from the Matlab 4 times, before CELL-strings allowed to create a container for strings of different lengths.
variables, constants, strings and cells
Too many assumptions for such small question but it's all ok
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)
c = 1×9 cell array
{'Addme to'} {'Addme each'} {'Addme one'} {'Addme of'} {'Addme these'} {'Addme words'} {'Addme with'} {'Addme a'} {'Addme space'}
a = 'Addme';
b = { 'to', 'each', 'one', 'of', 'these', 'words', 'with', 'a', 'space'};
strjoin([a, b])
ans = 'Addme to each one of these words with a space'
a + " " + b
ans = 1×9 string array
"Addme to" "Addme each" "Addme one" "Addme of" "Addme these" "Addme words" "Addme with" "Addme a" "Addme space"

请先登录,再进行评论。

更多回答(4 个)

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 个评论

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.
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.

请先登录,再进行评论。

Jy·Li
Jy·Li 2023-5-25

3 个投票

c=strcat(a,32,b); % the unicode value of ' ' is 32
Usman Nawaz
Usman Nawaz 2020-9-6

2 个投票

use double quotes instead of single quotes, worked for me.

1 个评论

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
R P 2011-6-11

0 个投票

Thank you, Walter

3 个评论

Please always test the answers provided before accepting them, Walter answer isn't correct (this time).
>> strcat({'word1'},{' '},{'word2'})
ans =
'word1 word2'
You can dereference this or cell2mat it if you want the string itself as output.
@Walter: CELL2MAT is not efficient here. S{1} is nicer.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Characters and Strings 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by