To get the length of a given string in Perl, you can use the length
function.
In this example,
$str1
with a string value of "Hello World"
.length
function to get the length of the string and store the returned value in the variable $length1
.$length1
to the console using print
function.# Length of "Hello World" String
my $str1 = "Hello World";
my $length1 = length($str1);
print "Length of given string is: $length1\n";
Length of given string is: 11
In this example,
$str2
with a string value of "apple"
.length
function to get the length of the string and store the returned value in the variable $length2
.$length2
to the console using print
function.# Length of "apple" String
my $str2 = "apple";
my $length2 = length($str2);
print "Length of given string is: $length2\n";
Length of given string is: 5
In this tutorial, we learned How to get Length of String in Perl language with well detailed examples.