可以使用矩阵表示法连接字符串(详见串, Character Arrays)这通常是最自然的方法。例如
fullname = [fname ".txt"]; email = ["<" user "@" domain ">"];
在每种情况下,都很容易看到最后的字符串会是什么样子。这种方法也是最有效的。当使用矩阵连接时,解析会立即开始连接字符串,而不必处理函数调用的开销和关联函数的输入验证。
这个newline
函数可用于连接字符串,使其在显示时显示为多行文本。
此外,还有其他几个用于连接字符串对象的函数,这些函数在特定情况下可能很有用:char
,strvcat
, strcat
和cstrcat
最后,可以使用通用连接函数:详见cat,horzcat和vertcat.
cstrcat
通过为每个元素(或多字节序列)取相应的UTF-8字符,将数字输入转换为字符数据,如下例所示:char ([98, 97, 110, 97, 110, 97]) ⇒ banana
有关区域设置编码和UTF-8之间的转换,详见unicode2native和native2unicode.
char
和strvcat
垂直连接,同时strcat
和cstrcat
横向连接。例如char ("an apple", "two pears") ⇒ an apple two pears
strcat ("oc", "tave", " is", " good", " for you") ⇒ octave is good for you
char
在输出中为输入中的每个空字符串生成一个空行。strvcat
,另一方面,消除了空字符串。char ("orange", "green", "", "red") ⇒ orange green red
strvcat ("orange", "green", "", "red") ⇒ orange green red
cstrcat
也接受cellarray数据(详见元胞数组). char
和strvcat
将元胞数组转换为字符数组,同时strcat
在元胞数组的单元内连接:char ({"red", "green", "", "blue"}) ⇒ red green blue
strcat ({"abc"; "ghi"}, {"def"; "jkl"}) ⇒ { [1,1] = abcdef [2,1] = ghijkl }
strcat
删除参数中的尾部空白(元胞数组内除外),同时cstrcat
保留空白。行为的Bothkinds可能很有用,如示例所示:strcat (["dir1";"directory2"], ["/";"/"], ["file1";"file2"]) ⇒ dir1/file1 directory2/file2
cstrcat (["thirteen apples"; "a banana"], [" 5$";" 1$"]) ⇒ thirteen apples 5$ a banana 1$
请注意,在上面的示例中cstrcat
,空白源自字符串数组中字符串的内部表示形式(详见字符数组).
C =
char (A)
¶C =
char (A, …)
¶C =
char (str1, str2, …)
¶C =
char (cell_array)
¶从一个或多个数字矩阵、字符矩阵或元胞数组创建字符串数组。
参数是垂直连接的。返回的值根据需要用空格填充,以使字符串数组的每一行具有相同的长度。空的输入字符串意义重大,将在输出中连接。
对于数字输入,每个元素都转换为相应的ASCII字符。如果输入超出ASCII范围(0-255),则会返回范围错误。
对于元胞数组,每个元素都是单独连接的。元胞数组转换通过char
大部分可以用转换returncellstr
例如
char ([97, 98, 99], "", {"98", "99", 100}, "str1", ["ha", "lf"]) ⇒ ["abc " " " "98 " "99 " "d " "str1" "half"]
C =
strvcat (A)
¶C =
strvcat (A, …)
¶C =
strvcat (str1, str2, …)
¶C =
strvcat (cell_array)
¶从一个或多个数字矩阵、字符矩阵或元胞数组创建字符数组。
参数是垂直连接的。返回的值根据需要用空格填充,以使字符串数组的每一行具有相同的长度。不像char
,空字符串将被删除,并且不会出现在输出中。
对于数字输入,每个元素都转换为相应的ASCII字符。如果输入超出ASCII范围(0-255),则会返回范围错误。
对于元胞数组,每个元素都是单独连接的。元胞数组转换通过strvcat
大部分可以用转换returncellstr
例如
strvcat ([97, 98, 99], "", {"98", "99", 100}, "str1", ["ha", "lf"]) ⇒ ["abc " "98 " "99 " "d " "str1" "half"]
str =
strcat (s1, s2, …)
¶返回一个字符串,其中包含水平连接的所有参数。
如果自变量是单元字符串,strcat
返回连接了各个数组的数组字符串。对于数字输入,每个元素都转换为相应的ASCII字符。在连接字符串之前,将消除任何字符串输入的尾随空白。请注意,数组字符串值会不修剪空白。
例如
strcat ("|", " leading space is preserved", "|") ⇒ | leading space is preserved|
strcat ("|", "trailing space is eliminated ", "|") ⇒ |trailing space is eliminated|
strcat ("homogeneous space |", " ", "| is also eliminated") ⇒ homogeneous space || is also eliminated
s = [ "ab"; "cde" ]; strcat (s, s, s) ⇒ "ababab " "cdecdecde"
s = { "ab"; "cd " }; strcat (s, s, s) ⇒ { [1,1] = ababab [2,1] = cd cd cd }
str =
cstrcat (s1, s2, …)
¶返回一个字符串,其中包含所有水平连接的参数,并保留尾部空白。
例如
cstrcat ("ab ", "cd") ⇒ "ab cd"
s = [ "ab"; "cde" ]; cstrcat (s, s, s) ⇒ "ab ab ab " "cdecdecde"
版权所有 © 2024-2025 Octave中文网
ICP备案/许可证号:黑ICP备2024030411号-2