If I have a Perl string:
$str = "Hello";
how can I get a character at a given index similar to charAt(index)?
Use charAt() to get a character at the specified index in the string. Alternatively, because strings can be treated like arrays, use the index via bracket notation. To get the character code of the character at a specified index, use charCodeAt() . Note that these methods are all getter methods (return a value).
The indexOf() method returns the position of the first occurrence of specified character(s) in a string. Tip: Use the lastIndexOf method to return the position of the last occurrence of specified character(s) in a string.
Perl | index() Function This function returns the position of the first occurrence of given substring (or pattern) in a string (or text).
$@ The Perl syntax error or routine error message from the last eval, do-FILE, or require command. If set, either the compilation failed, or the die function was executed within the code of the eval.
Use substr
with length 1 as in:
$nth = substr($string, n-1, 1);
Also lookup perlmonks for other solutions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With