in reply to Counting how many times a character is in a string.
Update: Nevermind, I was completely wrong here. Thanks to Aristotle for setting me straight. Apparently, I just breezed right over that section of the docs.
As has been mentioned several times, tr is the fastest and easiest way to get a count a particular character in a string. All of the examples up to this point have been destructive though. If you want to get a count without modifying the string, simply transliterate the character to itself.
my $str='abc%def%.%'; my $count = $str =~ tr/%/%/; print "\"$str\" has $count % in it.\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Counting how many times a character is in a string.
by Aristotle (Chancellor) on Nov 18, 2005 at 18:21 UTC | |
by bart (Canon) on Nov 19, 2005 at 14:36 UTC |