Perl Topics





Español Français 中文 Deutsch Portuguese Japanese nederlands
   
 
Perl Tutorial
Introduction Introduction
Installing Perl Installing Perl
Basics of Perl Basics of Perl
Scalar Variables Scalar Variables
Operators Operators
Control Structures Control Structures
Lists Lists
Array Array
Arrays Manipulation Arrays Manipulation
Arrays Functons Array Functions
Hash Hash
Hash Functions Hash Functions
String Functions String Functions
Regular Expression Regular Expression
Regular Expression Functions Regular Expression Functions
Numerical Functions Numerical Functions
List Functions List Functions
User Defined Function User Defined Function
File Handling File Handling
Forums Ask Your Doubts
Feedback Feedback
 





substr Function in Perl


Tutorials Perl

Topic

What is substr Function in Perl?



Explanation

This is used to return a substring from the expression supplied as it's first argument.

Syntax:
    substr EXPR,OFFSET,LENGTH,REPLACEMENT
    substr EXPR,OFFSET,LENGTH
    substr EXPR,OFFSET

In the above syntax's, "EXPR" is the string, "OFFSET" is the starting of the substring inside the string, "Length" specifies the length from the offset to the end of the string, "REPLACEMENT" specifies the string to be replaced with the substring.

Example:
    #! C:\programfiles\perl\bin\perl
    print "content-type: text/html\n\n";
    $s = "hi how are you";
    $str = substr ($s,3,,);
    print "String printed from the offset 3: $str\n";
    print "<br>";
Result:
     String printed from the offset 3: how are you 

In the above example the string, from the third index is printed,"length","replacement" string arguments are not sepcified to print the string.

Example:
    #! C:\programfiles\perl\bin\perl
    print "content-type: text/html\n\n";
    $s = "hi how are you";
    $str = substr ($s,3,3,);
    print "String printed from the offset 3: $str\n";
    print "<br>";
Result:
    Printed string with offset 3,upto length 3: how 

In the above substr function example,we have specifed the length as "3" apart from the offset "3", so only the string "how" is printed.

Example:
    #! C:\programfiles\perl\bin\perl
    print "content-type: text/html\n\n";
    $s = "hi how are you";
    $str = substr ($s,3,3,where);
    print "Printed string 'how' replaced with 'where': $s\n";
Result:
    Printed string 'how' replaced with 'where': hi where are you 

In the above substr function example, we have kept the "offset" and "length" same, but here we have added a string to replaced where the string "how" is replaced with the string "where".






A Note
Simple introduction, basic CGI perl programming codes with examples. Do send your feedback or suggestions on this tutorial. This is a copyright content.

Other Links

web hosting