Octave strcat ignores added spaces

不想你离开。 提交于 2019-12-10 19:20:03

问题


Octave adds spaces with strcat

In Octave I run these commands:

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

I get this result:

ans = hello

Instead of what I expected:

ans = hel lo

strcat to me sounds like "concatenate strings". A space is a valid character, so adding a space should be OK. Matlab has the same behaviour, so it's probably intended.

I find it counter intuitive. Does this behavior makes sense?


回答1:


Hmm. It works how it is defined:

"strcat removes trailing white space in the arguments (except within cell arrays), while cstrcat leaves white space untouched. "

From http://www.gnu.org/software/octave/doc/interpreter/Concatenating-Strings.html

So the question could be: Should this behaviour be changed.




回答2:


strcat takes the input parameters and trims the trailing spaces, but not the leading spaces. if you pass a parameter as one or more spaces, they are collapsed to blank string.

That behavior is a manifestation of how "cellstr" works where spaces at the end are removed.

Work around 1

If you put the space up against the 'lo', it is a leading space and not removed.

strcat ("hel", " lo")

ans = hel lo

Work around 2

use cstrcat instead:

cstrcat("hel", " ", "lo")

ans = hel lo

Work around 3

Use printf, can be faster than strcat.

printf("%s%s%s\n", "hel", " ", "lo")

hel lo


来源:https://stackoverflow.com/questions/14025985/octave-strcat-ignores-added-spaces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!