5.3.4 在字符串中搜索

由于字符串是一个字符数组,因此字符串之间的比较是按元素进行的,如下例所示:

GNU = "GNU's Not UNIX";
spaces = (GNU == " ")
     ⇒  spaces =
       0   0   0   0   0   1   0   0   0   1   0   0   0   0

要确定两个字符串是否相同,需要使用strcmp函数。它比较完整的字符串并且区分大小写。strncmp只比较前N个字符(N作为参数给出)。strcmpistrncmpi是用于不区分大小写比较的对应函数。

 
tf = strcmp (str1, str2)

如果字符串 str1str2 相同则返回 1,否则返回 0。

如果 str1str2 是字符串的元胞数组,则返回一个相同大小的数组,其中包含上述元胞数组每个成员对应的值。另一个参数也可以是字符串的元胞数组(大小相同或只有一个元素)、字符矩阵或字符串。

注意:为了与 MATLAB 兼容,Octave 的 strcmp 函数在字符串相等时返回 1,否则返回 0。这与相应的 C 库函数正好相反。

另请参阅:strcmpistrncmpstrncmpi

 
tf = strncmp (str1, str2, n)

如果字符串 str1str2 的前 n 个字符相同则返回 1,否则返回 0。

strncmp ("abce", "abcd", 3)
      ⇒  1

如果 str1str2 是字符串的元胞数组,则返回一个相同大小的数组,其中包含上述元胞数组每个成员对应的值。另一个参数也可以是字符串的元胞数组(大小相同或只有一个元素)、字符矩阵或字符串。

strncmp ("abce", {"abcd", "bca", "abc"}, 3)
     ⇒  [1, 0, 1]

注意:为了与 MATLAB 兼容,Octave 的 strncmp 函数在字符串相等时返回 true,否则返回 false。这与相应的 C 库函数正好相反。此外,Octave 的 strncmp 函数在 N = 0 时返回 true。

MATLAB 不兼容性:Octave 的 strncmp 函数在 N < 0 时会报错,而 MATLAB 将 N < 0 视为与 N = 0 相同,始终返回 true。

另请参阅:strncmpistrcmpstrcmpi

 
tf = strcmpi (str1, str2)

如果字符串 str1str2 相同(忽略字母大小写)则返回 1,否则返回 0。

如果 str1str2 是字符串的元胞数组,则返回一个相同大小的数组,其中包含上述元胞数组每个成员对应的值。另一个参数也可以是字符串的元胞数组(大小相同或只有一个元素)、字符矩阵或字符串。

注意:为了与 MATLAB 兼容,Octave 的 strcmpi 函数在字符串相等时返回 1,否则返回 0。这与相应的 C 库函数正好相反。

注意:不支持国家字母表。

另请参阅:strcmpstrncmpstrncmpi

 
tf = strncmpi (str1, str2, n)

如果 s1s2 的前 n 个字符相同(忽略字母大小写)则返回 1,否则返回 0。

如果 str1str2 是字符串的元胞数组,则返回一个相同大小的数组,其中包含上述元胞数组每个成员对应的值。另一个参数也可以是字符串的元胞数组(大小相同或只有一个元素)、字符矩阵或字符串。

注意:为了与 MATLAB 兼容,Octave 的 strncmpi 函数在字符串相等时返回 true,否则返回 false。这与相应的 C 库函数正好相反。此外,Octave 的 strncmpi 函数在 N = 0 时返回 true。

MATLAB 不兼容性:Octave 的 strncmpi 函数在 N < 0 时会报错,而 MATLAB 将 N < 0 视为与 N = 0 相同,始终返回 true。

注意:不支持国家字母表。

另请参阅:strncmpstrcmpstrcmpi

除了这些比较函数之外,还有更专门的函数用于查找字符串中搜索模式的索引位置。

 
retval = startsWith (str, pattern)
retval = startsWith (str, pattern, "IgnoreCase", ignore_case)

检查字符串是否以指定模式开头。

返回一个逻辑值数组,指示输入 str(单个字符串或字符串的元胞数组)中的哪些字符串以输入 pattern(单个字符串或字符串的元胞数组)开头。

如果参数 "IgnoreCase" 的值为 true,则函数将忽略 strpattern 的字母大小写。默认情况下,比较是区分大小写的。

示例:

## 一个字符串和一个模式,考虑大小写
startsWith ("hello", "he")
      ⇒   1

## 一个字符串和一个模式,忽略大小写
startsWith ("hello", "HE", "IgnoreCase", true)
      ⇒   1

## 多个字符串和多个模式,考虑大小写
startsWith ({"lab work.pptx", "data.txt", "foundations.ppt"},
            {"lab", "data"})
      ⇒   1  1  0

## 多个字符串和一个模式,考虑大小写
startsWith ({"DATASHEET.ods", "data.txt", "foundations.ppt"},
            "data", "IgnoreCase", false)
      ⇒   0  1  0

## 多个字符串和一个模式,忽略大小写
startsWith ({"DATASHEET.ods", "data.txt", "foundations.ppt"},
            "data", "IgnoreCase", true)
      ⇒   1  1  0

另请参阅:endsWithregexpstrncmpstrncmpi

 
retval = endsWith (str, pattern)
retval = endsWith (str, pattern, "IgnoreCase", ignore_case)

检查字符串是否以指定模式结尾。

返回一个逻辑值数组,指示输入 str(单个字符串或字符串的元胞数组)中的哪些字符串以输入 pattern(单个字符串或字符串的元胞数组)结尾。

如果参数 "IgnoreCase" 的值为 true,则函数将忽略 strpattern 的字母大小写。默认情况下,比较是区分大小写的。

示例:

## 一个字符串和一个模式,考虑大小写
endsWith ("hello", "lo")
      ⇒   1

## 一个字符串和一个模式,忽略大小写
endsWith ("hello", "LO", "IgnoreCase", true)
      ⇒   1

## 多个字符串和多个模式,考虑大小写
endsWith ({"tests.txt", "mydoc.odt", "myFunc.m", "results.pptx"},
          {".docx", ".odt", ".txt"})
      ⇒   1  1  0  0

## 多个字符串和一个模式,考虑大小写
endsWith ({"TESTS.TXT", "mydoc.odt", "result.txt", "myFunc.m"},
          ".txt", "IgnoreCase", false)
      ⇒   0  0  1  0

## 多个字符串和一个模式,忽略大小写
endsWith ({"TESTS.TXT", "mydoc.odt", "result.txt", "myFunc.m"},
          ".txt", "IgnoreCase", true)
      ⇒   1  0  1  0

另请参阅:startsWithregexpstrncmpstrncmpi

 
v = findstr (s, t)
v = findstr (s, t, overlap)

此函数已过时。请改用 strfind

返回两个字符串中较长字符串的所有位置向量 st,其中较短的字符串开始出现。

如果可选参数 overlap 为 true(默认值),则返回的向量可以包含重叠的位置。例如:

findstr ("ababab", "a")
     ⇒  [1, 3, 5];
findstr ("abababa", "aba", 0)
     ⇒  [1, 5]

注意:findstr 已过时。在所有新代码中请使用 strfind

另请参阅:strfindstrmatchstrcmpstrncmpstrcmpistrncmpifind

 
idx = strchr (str, chars)
idx = strchr (str, chars, n)
idx = strchr (str, chars, n, direction)
[i, j] = strchr (…)

在字符串 str 中搜索字符集 chars 中出现的字符。

返回值以及 ndirection 参数的行为与 find 中的完全相同。

在大多数情况下,这比使用 regexp 更快。

另请参阅:find

 
n = index (s, t)
n = index (s, t, direction)

返回字符串 t 在字符串 s 中首次出现的位置,如果未找到匹配项则返回 0。

s 也可以是字符串数组或字符串的元胞数组。

例如:

index ("Teststring", "t")
    ⇒  4

如果 direction"first",则返回找到的第一个元素。如果 direction"last",则返回找到的最后一个元素。

另请参阅:findrindex

 
n = rindex (s, t)

返回字符串 t 在字符串 s 中最后一次出现的位置,如果未找到匹配项则返回 0。

s 也可以是字符串数组或字符串的元胞数组。

例如:

rindex ("Teststring", "t")
     ⇒  6

rindex 函数等效于将 direction 设置为 "last"index 函数。

另请参阅:findindex

 
idx = unicode_idx (str)

返回一个数组,其中包含 str 中每个 UTF-8 编码字符的索引。

unicode_idx ("aäbc")
     ⇒  [1, 2, 2, 3, 4]
 
idx = strfind (str, pattern)
idx = strfind (cellstr, pattern)
idx = strfind (…, "overlaps", val)
idx = strfind (…, "forcecelloutput", val)

在字符串 str 中搜索 pattern,并在向量 idx 中返回每个匹配项的起始索引。

如果没有这样的匹配项,或者 patternstr 长,或者 pattern 本身为空,则 idx 为空数组 []

可选参数 "overlaps" 决定模式是否可以在 str 中的每个位置匹配(true),还是仅匹配完整模式的唯一出现(false)。默认值为 true。

如果指定了字符串的元胞数组 cellstr,则 idx 是一个向量的元胞数组,如上所述。

可选参数 "forcecelloutput" 强制 idx 作为向量的元胞数组返回。默认值为 false。

示例:

strfind ("abababa", "aba")
     ⇒  [1, 3, 5]

strfind ("abababa", "aba", "overlaps", false)
     ⇒  [1, 5]

strfind ({"abababa", "bebebe", "ab"}, "aba")
     ⇒ 
        {
          [1,1] =

             1   3   5

          [1,2] = [](1x0)
          [1,3] = [](1x0)
        }

strfind ("abababa", "aba", "forcecelloutput", true)
     ⇒ 
        {
          [1,1] =

             1   3   5
        }

另请参阅:regexpregexpifind

 
idx = strmatch (s, A)
idx = strmatch (s, A, "exact")

此函数已过时。请改用 strncmpstrcmp 等替代方案。

返回 A 中以字符串 s 开头的条目的索引。

第二个参数 A 必须是字符串、字符矩阵或字符串的元胞数组。

如果未给出第三个参数 "exact",则 s 只需匹配 A 的前 s 长度部分。匹配时忽略 sA 中的尾随空格和空字符。

例如:

strmatch ("apple", "apple juice")
     ⇒  1

strmatch ("apple", ["apple  "; "apple juice"; "an apple"])
     ⇒  [1; 2]

strmatch ("apple", ["apple  "; "apple juice"; "an apple"], "exact")
     ⇒  [1]

注意:strmatch 已过时(并且在 MATLAB 中与字符串的元胞数组一起使用时可能产生错误结果)。在所有新代码中请使用 strncmp(一般情况)或 strcmp"exact" 情况)。根据应用场景,其他替换方案包括 regexpvalidatestring

另请参阅:strncmpstrcmpregexpstrfindvalidatestring


版权所有 © 2024-2026 Octave中文网

ICP备案/许可证号:黑ICP备2024030411号-4