Discussion:
how to insert space between strings using strcat?
(too old to reply)
Jane
2006-08-21 22:22:00 UTC
Permalink
Hi,
I tried to use strcat to concatenate different strings, but seems
that strcat ignore blank strings. For example,
strcat('A','B') is the same as strcat('A',' ','B'). Does anybody
know how to create the space between different strings?

Thanks,
Jane
Richard Brown
2006-08-21 23:19:25 UTC
Permalink
Post by Jane
Hi,
I tried to use strcat to concatenate different strings, but seems
that strcat ignore blank strings. For example,
strcat('A','B') is the same as strcat('A',' ','B'). Does anybody
know how to create the space between different strings?
strcat chomps trailing spaces in strings. You might want to just use
matrix concatenation (horzcat):
['A', ' ', 'B']

otherwise, try sprintf for more flexibility

HTH

Richard
Michael Wild
2006-08-22 06:53:50 UTC
Permalink
Post by Richard Brown
Post by Jane
Hi,
I tried to use strcat to concatenate different strings, but seems
that strcat ignore blank strings. For example,
strcat('A','B') is the same as strcat('A',' ','B'). Does anybody
know how to create the space between different strings?
strcat chomps trailing spaces in strings. You might want to just use
['A', ' ', 'B']
otherwise, try sprintf for more flexibility
HTH
Richard
or enclose the string in {} which will tell strcat to respect spaces.

strcat({'hello '},{'Anna','Jane','Mark'});
or
strcat('hello',{' '},{'Anna','Jane','Mark'});


will give you what you want.


michael

michael
Steven Lord
2006-08-22 14:38:42 UTC
Permalink
Post by Jane
Hi,
I tried to use strcat to concatenate different strings, but seems
that strcat ignore blank strings. For example,
strcat('A','B') is the same as strcat('A',' ','B'). Does anybody
know how to create the space between different strings?
Yes, this is the documented behavior of STRCAT.

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/strcat.html

As stated in the third line of the Description section of that page, as well
as Richard and Michael's posts, use the cell array syntax or use plain old
concatenation with [].
--
Steve Lord
***@mathworks.com
Tsolmonbaatar Khurelbaatar
2013-04-25 05:21:08 UTC
Permalink
Thank you very much. It helped me a lot.

kaved jindal
2007-01-19 06:07:18 UTC
Permalink
Post by Jane
Hi,
I tried to use strcat to concatenate different strings, but seems
that strcat ignore blank strings. For example,
strcat('A','B') is the same as strcat('A',' ','B'). Does anybody
know how to create the space between different strings?
Thanks,
Jane
Michael Salloker
2007-01-19 10:19:33 UTC
Permalink
Post by Jane
Hi,
I tried to use strcat to concatenate different strings, but seems
that strcat ignore blank strings. For example,
strcat('A','B') is the same as strcat('A',' ','B'). Does anybody
know how to create the space between different strings?
Thanks,
Jane
From the strcat documentation:

Trailing spaces in character array inputs are ignored and do not appear
in the output. This is not true for inputs that are cell arrays of
strings. Use the concatenation syntax [s1 s2 s3 ...] to preserve
trailing spaces.

Michael
Loading...