Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
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 wandering the Monastery: (13)
As of 2014-10-23 12:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (125 votes), past polls