📜  Octave GNU 中的字符串

📅  最后修改于: 2022-05-13 01:55:02.963000             🧑  作者: Mango

Octave GNU 中的字符串

在 Octave GNU 中,字符串基本上是用双引号 (") 或单引号 (') 括起来的字符的集合。

字符串示例

在 Octave 中,字符串的长度没有限制。即它可以是任何大小。字符数组用于在 Octave 中表示一个字符串

转义序列:一些包含反斜杠('\')的双引号(“)字符串,改变该字符串常量的含义,这些类型的字符串是特殊的并且用于特殊目的。反斜杠 ('\')字符称为转义字符

下面是一些转义序列的列表

S.No.

Escape Sequence

Meaning

1

\\

Represents a literal backslash, ‘\’
2

\”

Represents a literal double-quote character, ‘”‘
3

\’

Represents a literal single-quote character, ”’
4

\0

Represents the null character, ASCII code 0
5

\a

Represents the “alert” character, ASCII code 7
6

\b

Represents a backspace, ASCII code 8
7

\f

Represents a formfeed, ASCII code 12
8

\n

Represents a newline, ASCII code 10
9

\r

Represents a carriage return, ASCII code 13
10

\t

Represents a horizontal tab, ASCII code 9
11

\v

Represents a vertical tab, ASCII code 11
12

\nnn

Represents the octal value nnn, where nnn are one to three digits between 0

and 7

13

\xhh…

Represents the hexadecimal value hh, where hh are hexadecimal digits (‘0’

through ‘9’ and either ‘A’ through ‘F’ or ‘a’ through ‘f’).

在 Octave 中创建字符串

在octave中,可以使用双引号、单引号blanks ()生成字符串

  • 使用双引号: varA = “String”;
  • 使用单引号: varB = 'String';
  • 使用 blanks() : varC = blanks(10),创建一个 10 大小的空白字符串,相当于“”

Octave中的字符串连接

在八度中,有两种连接字符串的方法

  • 使用方括号'[]': newStr = [oldStr1 oldStr2];或 newStr = [oldStr1, oldStr2];
  • 使用 strcat() : newStr = strcat(oldStr1, oldStr2);

Octave中的字符串比较

在 octave 中, strcmp()用于比较两个字符串

strcmp()有多种版本:

  • strcmp(s1, s2, n) :比较 s1 和 s2 的前 n 个字符
  • strcmpi(s1, s2) :不区分大小写strcmp()

下面是用于演示上述功能和概念的 octave 代码

% String creation in Octave
str1 = "This is a string";
str2 = 'This is also a string';
str3 = blanks(10);
printf("String created using \" is : %s .\n", str1);
printf("String created using \' is : %s .\n", str2);
printf("String created using blanks() is : %s .\n\n", str3);
   
% String Concatenation in Octave
display("String Concatenation")
oldStr1 = "first";
oldStr2 = "last";
newStr1 = [oldStr1 oldStr2];
newStr2 = [oldStr1, oldStr2];
newStr3 = strcat(oldStr1, oldStr2);
printf("String Concatenation using [ ] is : %s .\n", newStr1);
printf("String Concatenation using [, ] is : %s .\n", newStr2);
printf("String Concatenation using strcat() is : %s .\n\n", newStr3);
   
% String comparison in Octave
display("String Comparision")
printf("Comparision using strcmp() : ");
disp(strcmp(str1, str2));

输出:

String created using " is : This is a string .
String created using ' is : This is also a string .
String created using blanks() is :            .

String Concatenation
String Concatenation using [ ] is : firstlast .
String Concatenation using [, ] is : firstlast .
String Concatenation using strcat() is : firstlast .

String Comparision
Comparision using strcmp() : 0