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)

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

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

小心为了与兼容MATLAB,如果字符串相等,Octave的strcmp函数返回1,否则返回0。这与相应的C库函数正好相反。

详见: strcmpi, strncmp, strncmpi.

广告
 
: tf = strncmp (str1, str2, n)

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

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

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

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

小心为了与兼容MATLAB,如果字符串相等,Octave的strncmpfunction将返回1,否则返回0。这与相应的C库函数正好相反。

详见: strncmpi, strcmp, strcmpi.

广告
 
: tf = strcmpi (str1, str2)

如果字符串为,则返回1str1str2是相同的,不考虑字母字符的大小写,否则为0。

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

小心为了与兼容MATLAB,如果字符串相等,Octave的strcmp函数返回1,否则返回0。这与相应的C库函数正好相反。

小心不支持国家字母。

详见: strcmp, strncmp, strncmpi.

广告
 
: tf = strncmpi (str1, str2, n)

如果第一个返回1n的属性s1s2是相同的,不考虑字母字符的大小写,否则为0。

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

小心为了与兼容MATLAB,如果字符串相等,Octave的strncmpi函数返回1,否则返回0。这与相应的C库函数正好相反。

小心不支持国家字母。

详见: strncmp, strcmp, strcmpi.

广告

尽管有这些比较函数,但还有更专门的函数来查找字符串中搜索模式的索引位置。

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

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

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

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

示例:

## one string and one pattern while considering case
startsWith ("hello", "he")
      ⇒  1

## one string and one pattern while ignoring case
startsWith ("hello", "HE", "IgnoreCase", true)
      ⇒  1

## multiple strings and multiple patterns while considering case
startsWith ({"lab work.pptx", "data.txt", "foundations.ppt"},
            {"lab", "data"})
      ⇒  1  1  0

## multiple strings and one pattern while considering case
startsWith ({"DATASHEET.ods", "data.txt", "foundations.ppt"},
            "data", "IgnoreCase", false)
      ⇒  0  1  0

## multiple strings and one pattern while ignoring case
startsWith ({"DATASHEET.ods", "data.txt", "foundations.ppt"},
            "data", "IgnoreCase", true)
      ⇒  1  1  0

详见: endsWith, regexp, strncmp, strncmpi.

广告
 
: retval = endsWith (str, pattern)
: retval = endsWith (str, pattern, "IgnoreCase", ignore_case)

检查字符串是否以模式结束。

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

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

示例:

## one string and one pattern while considering case
endsWith ("hello", "lo")
      ⇒  1

## one string and one pattern while ignoring case
endsWith ("hello", "LO", "IgnoreCase", true)
      ⇒  1

## multiple strings and multiple patterns while considering case
endsWith ({"tests.txt", "mydoc.odt", "myFunc.m", "results.pptx"},
          {".docx", ".odt", ".txt"})
      ⇒  1  1  0  0

## multiple strings and one pattern while considering case
endsWith ({"TESTS.TXT", "mydoc.odt", "result.txt", "myFunc.m"},
          ".txt", "IgnoreCase", false)
      ⇒  0  0  1  0

## multiple strings and one pattern while ignoring case
endsWith ({"TESTS.TXT", "mydoc.odt", "result.txt", "myFunc.m"},
          ".txt", "IgnoreCase", true)
      ⇒  1  0  1  0

详见: startsWith, regexp, strncmp, strncmpi.

广告
 
: v = findstr (s, t)
: v = findstr (s, t, overlap)

此函数已过时。使用strfind相反

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

如果可选参数overlap如果为true(默认值),则returnedvector可以包括重叠的位置。例如

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

小心 findstr已过时。使用strfind在所有新代码中。

详见: strfind, strmatch, strcmp, strncmp, strcmpi, strncmpi, find.

广告
 
: 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",返回找到的最后一个元素。

详见: find, rindex.

广告
 
: n = rindex (s, t)

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

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

例如

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

这个rindex函数等效于index具有direction设置为"last".

详见: find, index.

广告
 
: idx = unicode_idx (str)

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

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)

搜索pattern在字符串中str并返回向量中每一个此类事件的起始索引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
        }

详见: regexp, regexpi, find.

广告
 
: idx = strmatch (s, A)
: idx = strmatch (s, A, "exact")

此函数已过时。使用替代方案例如strncmpstrcmp相反

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

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

如果第三个参数"exact"没有给出,那么s只需要匹配A最长可达s.中的尾随空格和nullsA匹配时忽略。

例如

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"case)。根据应用情况,其他更换可能性包括regexpvalidatestring.

详见: strncmp, strcmp, regexp, strfind, validatestring.

广告

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

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