Perl: String Operations

By Xah Lee. Date: . Last updated: .

Substring

String extraction is done with substr(). The form is: substr($myString, offset index, number of characters to extract).

# -*- coding: utf-8 -*-
# perl

# substring. 2nd arg  is begin index, 3rd arg is count
print substr('012345', 2, 2); # prints 23

String Length

Length of string is length().

print length('abc');

String Join and Repetition

In Perl, string join is done with a dot.

$astr= "under" . "stand";

String repetition is done with the operator x.

print 'abc' x 2;