Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
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 chilling in the Monastery: (7)
As of 2015-08-31 18:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The oldest computer book still on my shelves (or on my digital media) is ...













    Results (361 votes), past polls