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

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.

主站蜘蛛池模板: 保定市| 苏州市| 岫岩| 子洲县| 迭部县| 郸城县| 怀宁县| 乌拉特中旗| 呼和浩特市| 哈尔滨市| 闵行区| 两当县| 沙洋县| 缙云县| 黔南| 萨嘎县| 汶川县| 方山县| 始兴县| 确山县| 尚义县| 焦作市| 泾源县| 沁源县| 浦县| 迁安市| 屏南县| 浠水县| 东兰县| 伽师县| 平昌县| 清徐县| 泗阳县| 陆丰市| 江北区| 毕节市| 枣强县| 彭山县| 榕江县| 汕尾市| 新蔡县|