|
Posted by J. Gleixner on September 11, 2008, 12:06 pm
Please log in for more thread options
hofer wrote:
> Hi,
> Let's look at following example:
>
> $text = "Today is a nice day";
> $end = "day";
>
> print "text ends with $end" if $text =~ /$end$/;
>
> Would the regular expression be efficient for long strings?
Why not benchmark some different alternatives to see? Your 'long
strings' might not be all that long.
>
> The alternative is a little more awkward to type
>
> print "text ends with $end" substr($text,-length($end)) eq $end; # I
> didn't try this line, but it should work I think
>
> Is there any core module containing something like
> print "text ends with $end" if endswith($text,$end);
Don't know if it'll be faster, but using length and index would be an
alternative, another would be substr.
perldoc -f index
perldoc -f length
perldoc -f substr
|