In Perl, you can use array-like indexing or the substr
function to get the first character of a string.
In this example,
$str
with a value of "Hello World"
.0
on the string $str
or the substr
function with parameters 0
and 1
to get the first character.$str = "Hello World";
$first_char = substr($str, 0, 1);
print "First character: $first_char\n";
First character: H
In this example,
$str
with a value of "apple"
.0
on the string $str
or the substr
function with parameters 0
and 1
to get the first character.$str = "apple";
$first_char = substr($str, 0, 1);
print "First character: $first_char\n";
First character: a
In this tutorial, we learned How to get First Character in String in Perl language with well detailed examples.