Copyright © tutorialspoint.com

PERL substr Function


Syntax

substr EXPR, OFFSET, LEN, REPLACEMENT

substr EXPR, OFFSET, LEN

substr EXPR, OFFSET


Definition and Usage

Returns a substring of EXPR, starting at OFFSET within the string. If OFFSET is negative, starts that many characters from the end of the string. If LEN is specified, returns that number of bytes, or all bytes up until end-of-string if not specified. If LEN is negative, leaves that many characters off the end of the string.

If REPLACEMENT is specified, replaces the substring with the REPLACEMENT string.

If you specify a substring that passes beyond the end of the string, it returns only the valid element of the original string.

Return Value

  • String

Example

Try out following example:

#!/usr/bin/perl -w

$temp = substr("okay", 2);
print "Substring valuye is $temp\n";

$temp = substr("okay", 1,2);
print "Substring valuye is $temp\n";

It will produce following result:

Substring valuye is ay
Substring valuye is ka

Copyright © tutorialspoint.com