- 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 11, Regexes, 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.
- Getting Started with Gulp(Second Edition)
- Learn TypeScript 3 by Building Web Applications
- C++程序設計(第3版)
- 劍指JVM:虛擬機實踐與性能調優
- Dependency Injection in .NET Core 2.0
- PHP網絡編程學習筆記
- Android程序設計基礎
- 深入理解Elasticsearch(原書第3版)
- Flowable流程引擎實戰
- Qlik Sense? Cookbook
- 并行編程方法與優化實踐
- HTML+CSS+JavaScript網頁制作:從入門到精通(第4版)
- 現代C:概念剖析和編程實踐
- 零基礎學C++(升級版)
- IPython Interactive Computing and Visualization Cookbook