Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Answer: How can I find the first position of a substring case-insensitively?

( #295598=categorized answer: print w/ replies, xml ) Need Help??

Q&A > strings > How can I find the first position of a substring case-insensitively? contributed by ambrus

You could use regexps, like this:

$needle = "a"; $haystack =~ /\Q$needle\E/i; $index = $-[0];
this works even if $needle has regexp-metacharacters in it.

But @- exists only in newer perls (from 5.6.1 I think). In perl 5.005 you could use something like

$needle = "a"; $haystack =~ /\Q$needle\E/i; $index = pos($haystack)-length($needle);
or
$needle = "a"; $haystack =~ /\Q$needle\E/i; $index = length($`);
but note that $` makes perl slower.

Whitespace added by tye

Comment on Answer: How can I find the first position of a substring case-insensitively?
Select or Download Code
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2015-07-04 05:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (57 votes), past polls