Chapter 7. Strings

Strings are not like integers, rationals, and Booleans. A string is a sequence of characters, which means it is an ordered collection of other values, and you sometimes need to access to some of these individual values. In this chapter you’ll see how to analyze, handle, and modify strings, and you’ll learn about some of the methods strings provide. You will also start to learn about a very powerful tool for manipulating text data: regular expressions, a.k.a. regexes.

A String Is a Sequence

A string is primarily a piece of textual data, but it is technically an ordered sequence of characters.

Many programming languages allow you to access individual characters of a string with an index between brackets. This is not directly possible in Perl, but you still can access the characters one at a time using the comb built-in method and the bracket operator:

> my $string = "banana";
banana
> my $st = $string.comb;
(b a n a n a)
> say $st[1];
a
> say $st[2];
n

The comb in the second statement splits the string into a list of characters that you can then access individually with square brackets.

The expression in brackets is called an index (it is sometimes also called a subscript). The index indicates which character in the sequence you want (hence the name). But this may not be what you expected: the item with index 1 is the second letter of the word. For computer scientists, the index is usually an offset from the beginning. The offset of the first letter (“b”) is ...

Get Think Perl 6 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.