strcat and spaces?

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

strcat and spaces?

Søren Hauberg
Hi All,
  If I do the following

    strcat ("hel", " ", "lo")

I get the string "hello", but I would have expected "hel lo". It seems
Matlab has the same behavior, so I guess this isn't a bug, but I find it
weird. Can anybody explain why this behavior makes sense?

Søren

Reply | Threaded
Open this post in threaded view
|

Re: strcat and spaces?

bpabbott
Administrator
On Wednesday, November 26, 2008, at 09:13AM, "Søren Hauberg" <[hidden email]> wrote:

>Hi All,
>  If I do the following
>
>    strcat ("hel", " ", "lo")
>
>I get the string "hello", but I would have expected "hel lo". It seems
>Matlab has the same behavior, so I guess this isn't a bug, but I find it
>weird. Can anybody explain why this behavior makes sense?
>
>Søren
>

That feature is a manifestation of how "cellstr" works. Specifically, all trailing spaces are trimmed.

octave:140> fprintf("\"%s\"\n",cellstr("  1  "){1})
"  1"

Thus,

octave:141> strcat ("hel", " ", "lo")
ans = hello
octave:142> strcat ("hel", " ", " lo")
ans = hel lo
octave:143>

Ben

Reply | Threaded
Open this post in threaded view
|

Re: strcat and spaces?

Søren Hauberg
ons, 26 11 2008 kl. 10:54 -0500, skrev Ben Abbott:
> That feature is a manifestation of how "cellstr" works. Specifically, all trailing spaces are trimmed.

Okay, fair enough. So, the obvious follow-up question is: why does
'cellstr' behave like that? I get that this is the compatible behavior,
but I'm just trying to see the reasoning behind this. Is this some great
feature that I simply fail to appreciate?

Søren

Reply | Threaded
Open this post in threaded view
|

Re: strcat and spaces?

bpabbott
Administrator
In reply to this post by Søren Hauberg

On Wednesday, November 26, 2008, at 11:24AM, "Søren Hauberg" <[hidden email]> wrote:

>ons, 26 11 2008 kl. 10:54 -0500, skrev Ben Abbott:
>> That feature is a manifestation of how "cellstr" works. Specifically, all trailing spaces are trimmed.
>
>Okay, fair enough. So, the obvious follow-up question is: why does
>'cellstr' behave like that? I get that this is the compatible behavior,
>but I'm just trying to see the reasoning behind this. Is this some great
>feature that I simply fail to appreciate?
>
>Søren
>

I think it is due to the need to pad spaces/blanks at the end of character data in a matrix format.

octave:155>  c = ["one";"two";"three"]
c =

one  
two  
three

octave:156> whos c
Variables in the current scope:

  Attr Name        Size                     Bytes  Class
  ==== ====        ====                     =====  =====
       c           3x5                         15  char

Total is 15 elements using 15 bytes

octave:150> s = cellstr(c)
s =

{
  [1,1] = one
  [2,1] = two
  [3,1] = three
}

octave:151> numel(s{1})
ans =  3
octave:152> numel(s{2})
ans =  3
octave:153> numel(s{3})
ans =  5

When converted to cells the trailing spaces/blanks are "deblanked".

Then we can come full circle with

octave:154> c = char(s)
c =

one  
two  
three


Ben


Reply | Threaded
Open this post in threaded view
|

Re: strcat and spaces?

Søren Hauberg
ons, 26 11 2008 kl. 11:33 -0500, skrev Ben Abbott:
> I think it is due to the need to pad spaces/blanks at the end of
> character data in a matrix format.

Ah yes, I understand. It makes sense that 'cellstr' behaves like that
for character matrices, which in turn makes it reasonable for ordinary
strings.

I'm not a fan of this behavior, but at least it makes some sense :-)

Thanks,
Søren

Reply | Threaded
Open this post in threaded view
|

Re: strcat and spaces?

John W. Eaton-6
On 26-Nov-2008, Søren Hauberg wrote:

| ons, 26 11 2008 kl. 11:33 -0500, skrev Ben Abbott:
| > I think it is due to the need to pad spaces/blanks at the end of
| > character data in a matrix format.
|
| Ah yes, I understand. It makes sense that 'cellstr' behaves like that
| for character matrices, which in turn makes it reasonable for ordinary
| strings.
|
| I'm not a fan of this behavior, but at least it makes some sense :-)

Don't blame us for it...

We added cstrcat so we could have the behavior you expect for the case
you originally mentioned.

jwe

Reply | Threaded
Open this post in threaded view
|

Re: strcat and spaces?

Søren Hauberg
ons, 26 11 2008 kl. 11:52 -0500, skrev John W. Eaton:
> | I'm not a fan of this behavior, but at least it makes some sense :-)
>
> Don't blame us for it...

Oh, I'd never do such a thing. A fairly good rule of thumb is that when
you experience something in Octave that is truly odd, then it's because
Mathworks, made some "interesting" decisions :-)

> We added cstrcat so we could have the behavior you expect for the case
> you originally mentioned.

Thanks, I didn't know that. I'll use that in the future.

Søren