Thursday, June 18, 2009

Perl: how to get position of a certain character in a string

There is a function called strpos() at php for this, and if you are looking for the similar function support at perl, then you have index() at Perl.

The index() function takes string and search string as parameter. Here is the example code.
$string = "tester of the world";
$search_string = "r";
print index($string, $search_string);


This will output 5, as 5 is the first occurrence or search string from the provided string. You can use character, or string as search. And remember the index starts from 0. At above example r is at the 5th index.

No comments:

Get function name programaticaly - Python

This little piece of code will help you to get the function name programatically. This is very helpful when you are implementing the debug...