官术网_书友最值得收藏!

  • Perl 6 Deep Dive
  • Andrew Shitov
  • 191字
  • 2021-07-03 00:05:53

Methods to check the content of a string

The Str class defines a pair of methods—starts-with and ends-with—that check whether the string contains a given substring at the beginning or end of it, and return a Boolean value. Consider the following example, which displays the behavior of these methods:

say 'Hello, World'.starts-with('Hello'); # True
say 'Hello, World'.starts-with('World'); # False

say 'Hello, World'.ends-with('Hello'); # False
say 'Hello, World'.ends-with('World'); # True

Regular expressions can be used instead of starts-with and end-with; refer to Chapter 11Regexes, for the details.

Another set of functions—index, rindex, and indices—find the substring and return its position. The index method finds the most left occurrence of the substrings, rindex searches from the end of the strings, and indices returns a list of indices of all occurrences of the substring.

my $town = 'Baden-Baden';

say $town.index('Baden'); # 0
say $town.rindex('Baden'); # 6
say $town.indices('Baden'); # (0 6)

It is worth noting that, while the rindex method searches from the end of the string, it returns an index of the character that is counted from left to right.

主站蜘蛛池模板: 靖西县| 苍溪县| 信宜市| 大悟县| 股票| 伊通| 东平县| 延庆县| 秭归县| 蛟河市| 晴隆县| 兴和县| 普定县| 道真| 永州市| 监利县| 陇南市| 阿荣旗| 泰州市| 乐至县| 桃园县| 屯门区| 新营市| 平安县| 河间市| 瑞安市| 辰溪县| 永春县| 清水县| 南和县| 禄丰县| 林西县| 五家渠市| 楚雄市| 吉隆县| 淮滨县| 岢岚县| 遵化市| 大竹县| 驻马店市| 桐乡市|