Octave - strcat() 在使用单个参数调用时不修剪尾随空格
Octave - strcat() not trimming trailing whitespace when called with a single argument
为什么只有一个参数的 strcat()
无法 trim 尾随空格?
我正在尝试使用 strcat() 来 trim 字符数组中的尾随空格:
s = "cheese ";
s = strcat(s);
但是这个 returns s = "cheese "
,即空格没有变化。如果我向 strcat() 添加第二个参数,例如现在 s = strcat(s, "");
s = "cheese"
。
我这样做,而不是调用 strtrim(),因为 zyBooks 教科书明确希望我们如何为单个字符串完成此任务——他们的示例显示 strcat( ) 以这种方式使用。我忽略了什么?
您的代码确实在 Matlab 中删除了尾随空格,但在 Octave 中没有。有时会发生这种情况:Octave 的函数或多或少像 Matlab 的函数,但有一些不同。有时这些差异是无意的,有时则不是。
在这种情况下,我认为您遇到的行为是 Octave 函数中的错误,因为它 documentation 说
Trailing white space for any character string input is eliminated before the strings are concatenated.
当只有一个字符串作为输入时,这不是它正在做的事情。
This Bug was reported on savannah and fixed on the default branch 几个小时后。
您可以等待包含此更改的下一个版本,或者只需下载 strcat.m from the mercurial repo 并在您的 Octave 安装中替换 strcat.m。
为什么只有一个参数的 strcat()
无法 trim 尾随空格?
我正在尝试使用 strcat() 来 trim 字符数组中的尾随空格:
s = "cheese ";
s = strcat(s);
但是这个 returns s = "cheese "
,即空格没有变化。如果我向 strcat() 添加第二个参数,例如现在 s = strcat(s, "");
s = "cheese"
。
我这样做,而不是调用 strtrim(),因为 zyBooks 教科书明确希望我们如何为单个字符串完成此任务——他们的示例显示 strcat( ) 以这种方式使用。我忽略了什么?
您的代码确实在 Matlab 中删除了尾随空格,但在 Octave 中没有。有时会发生这种情况:Octave 的函数或多或少像 Matlab 的函数,但有一些不同。有时这些差异是无意的,有时则不是。
在这种情况下,我认为您遇到的行为是 Octave 函数中的错误,因为它 documentation 说
Trailing white space for any character string input is eliminated before the strings are concatenated.
当只有一个字符串作为输入时,这不是它正在做的事情。
This Bug was reported on savannah and fixed on the default branch 几个小时后。
您可以等待包含此更改的下一个版本,或者只需下载 strcat.m from the mercurial repo 并在您的 Octave 安装中替换 strcat.m。