Perl String Functions
(1) lc
Converts all characters in the string to lower case.
my $test = lc("ABCD");
print $test; # print abcd
(2) uc
Converts all characters in the string to upper case.
my $test = uc("abcd");
print $test # print ABCD
(3) ucfirst
Takes a string and retruns it with the first character in upper case.
my $test = ucfirst("abcd");
print $test # print Abcd
(4) lcfirst
Takes a string and retruns it with the first character in lower case.
my $test = lcfirst("ABCD");
print $test # print aBCD
(5) length
Returns the length of the string in bytes.
my $length = length("ABCD");
print $length # print 4
(6) split
The split function is use to break up strings into a list of substrings.
my $name = "Lisa Mona Tony Austin";
my @array = split ( /\s/, $name );
print @array; # print LisaMonaTonyAustin
print $array[0]; # print Lisa
(7) join
The join does the inverse of split. Join takes a string that specifies the delimiter to be
concatenated between each item in the list supplied by subsequent parameter(s).
my $name = "Lisa Mona Tony Austin";
my @array = split ( /\s/, $name );
my $string = join ( ':', @array );
print $string; # print Lisa:Mona:Tony:Austin
(8) chop
This function removes the last character of a string and returns that character. If given a
list of arguments, the operation is performed on each one and the last character chopped is
returned.
Examples of chop
my $test = "abcdefghij";
chomp($test);
print $test; #would return exact string... nothing to remove
my $test = "abcdefghij\n";
chomp($test);
print $test; #would return 'abcdefghij', removed newline
my $test = "abcdefghij\n";
my $value = chomp($test);
print $value; #would return 1, it did remove something for sure
my $test = "abcdefghij";
chop($test);
print $test; #this would return 'abcdefghi'
my $test = "abcdefghij";
my $check= chop($test);
print $check; #this would return 'j'
(9) chomp
This is an alternative to the chop() function. It removes characters at the end of strings
corresponding to the $INPUT_LINE_SEPARATOR ($/). It returns the number of characters removed.
It can be given a list of strings upon which to perform this operation. When given no arguments,
the operation is performed on $_.
$/='abc';
$_='lafbabc';
chomp;
print; ##prints 'lafb', removes 'abc' as expected
(10) ord
The ord function return the ASCII value of any alphabet, numeric, special character you entered.
my $value = "A";
print ord($value);
#it will print 65