Skip to content

Commit

Permalink
Merge pull request esamattis#150 from lfac-pt/master
Browse files Browse the repository at this point in the history
Implement count without split
  • Loading branch information
rwz committed Oct 16, 2012
2 parents 1d7d2a4 + 4a82f83 commit f7caf43
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion lib/underscore.string.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,23 @@

count: function(str, substr){
if (str == null || substr == null) return 0;
return String(str).split(substr).length - 1;

str = String(str);
substr = String(substr);
var n = 0;
var pos = 0;
var l = substr.length;

while (true) {
pos = str.indexOf(substr, pos);
if (pos !== -1) {
n++;
pos += l;
} else {
break;
}
}
return n;
},

chars: function(str) {
Expand Down

0 comments on commit f7caf43

Please sign in to comment.