Perl/Substr

From Wsms

Jump to: navigation, search

parent:Perl

Note: This page should be titled substr (all lowercase). It is Substr due to technical limitations of Mediawiki.

substr is a perl-bultin that extracts substrings from scalars.

Simple usage:

#!/usr/bin/env perl
use strict;
use warnings;
$|++;

my $s = "one two three";
my $s1 = substr($s, 4, 3);
print "$s1\n";

my $n = 400;
my $n1 = substr($n, 0, 1);
print "$n1\n";

produces:

ggeller@harrison:~/Desktop/tmp$ ./substr_demo.pl
two
4

Note that the counting is zero-based and that there is automatic integer to string conversion.

see also

perldoc -f substr

Personal tools