Perl | Useful String functions
A string in Perl is a scalar variable and start with a ($) sign and it can contain alphabets, numbers, special characters. The string can consist of a single word, a group of words or a multi-line paragraph. The String is defined by the user within a single quote (‘) or double quote (“).
Perl provides various functions to manipulate the string like any other programming language.
Example:
# Perl program to demonstrate # string length function # string my $s = "geeksforgeeks" ; # using length function & # displaying length print ( "Length: " , length ( $s )); # Converting to Uppercase print ( "\nTo Upper Case: " ); print ( uc ( $s ), "\n" ); |
Output:
Length: 13 To Upper Case: GEEKSFORGEEKS
Some string functions of Perl are as follows:
Function | Description |
---|---|
chomp() | Used to remove the last trailing newline from the input string |
length() | Finds length (number of characters) of a given string, or $_ if not specified |
substr() | Returns a substring out of the string passed to the function starting from a given index up to the length specified |
uc() | Returns the string passed to it after converting it into uppercase |
ucfirst() | Returns the string VAR or $_ after converting the First character to uppercase |
lc() | Returns a lowercased version of VAR, or $_ if VAR is omitted |
lcfirst() | Returns the string VAR or $_ after converting the First character to lowercase |
chr() | Returns a string representing a character whose Unicode code point is an integer |
chop() | Returns a string representing a character whose Unicode code point is an integer |
index() | Returns the position of the first occurrence of given substring (or pattern) in a string (or text) |
rindex() | Returns the position of the last occurrence of the substring (or pattern) in the string (or text) |
sprintf() | Uses Format provided by the user to return the formatted string with the use of the values in the list |
ord() | Returns the ASCII value of the first character of a string |
quotemeta() | It escapes all meta-characters in the value passed to it as parameter |
split() | Used to split or cut a string into smaller sections or pieces |
Please Login to comment...