Perl substr Function
This function 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.
Following is the simple syntax for this function −
substr EXPR, OFFSET, LEN, REPLACEMENT substr EXPR, OFFSET, LEN substr EXPR, OFFSET
This function returns string.
Following is the example code showing its basic usage −
#!/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";
When above code is executed, it produces the following result −
Substring valuye is ay Substring valuye is ka