To get a character at a specific index in a string in Perl, you can use array-like indexing or the []
operator.
In this example,
$str
with a value of "Hello World"
.[]
operator on the string $str
.$str = "Hello World";
$char_at_index_5 = substr($str, 5, 1);
print "Character at index 5: $char_at_index_5\n";
Character at index 5: (space)
In this example,
$str
with a value of "Perl"
.[]
operator on the string $str
.$str = "Perl";
$char_at_index_1 = substr($str, 1, 1);
print "Character at index 1: $char_at_index_1\n";
Character at index 1: e
In this tutorial, we learned How to get Character at Specific Index in String in Perl language with well detailed examples.