0

I have a string

file123,file456,file789

I want to count the number of time "," is in this string for example for this string the answer should be 2

Gwenc37
  • 2,064
  • 7
  • 18
  • 22
AddyProg
  • 2,960
  • 13
  • 59
  • 110

6 Answers6

3

Simple regex will give you the length:

var str = "file123,file456,file789";
var count = str.match(/,/g).length;
Abimbola Esuruoso
  • 3,955
  • 1
  • 19
  • 25
  • I like this one, though it will generate an array of matches which is then discarded :) – Ja͢ck Apr 30 '14 at 10:10
  • @Jack So does `.split` and [this is a tiny bit faster](http://jsperf.com/count-occurences-of-string-in-string). – h2ooooooo Apr 30 '14 at 10:14
  • @h2ooooooo Yeah, exec() (my answer) comes in at a [relatively close second](http://jsperf.com/count-occurences-of-string-in-string/3) :) – Ja͢ck Apr 30 '14 at 10:19
  • @Jack `exec()` is actually third for me with ~3K ops/sec where `split()` and `match()` have about ~4.5K ops/sec. – h2ooooooo Apr 30 '14 at 10:21
  • @h2ooooooo I meant second in the sense that both split() and match() are kind of the same thing :) – Ja͢ck Apr 30 '14 at 10:22
  • @Jack Oh yes, then I completely agree. It's definitely faster than iterating the string :) – h2ooooooo Apr 30 '14 at 10:22
  • @h2ooooooo Yeah, vanilla iteration sucks... :'( – thefourtheye Apr 30 '14 at 10:23
  • @thefourtheye I wonder why that is - perhaps js just isn't good at looking up characters in a string? Perhaps it's just V8 taking over a good part of the match/split functions. – h2ooooooo Apr 30 '14 at 10:26
2

You can use RegExp.exec() to keep memory down if you're dealing with a big string:

var re = /,/g,
str = 'file123,file456,file789',
count = 0;

while (re.exec(str)) {
    ++count;
}

console.log(count);

Demo - benchmark comparison

The performance lies about 20% below a solution that uses String.match() but it helps if you're concerned about memory.

Update

Less sexy, but faster still:

var count = 0,
pos = -1;

while ((pos = str.indexOf(',', pos + 1)) != -1) {
  ++count;
}

Demo

Community
  • 1
  • 1
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
2

There are lots of ways you can do this, but here's a few examples :)

1.

"123,123,123".split(",").length-1

2.

("123,123,123".match(/,/g)||[]).length
Gillespie
  • 2,228
  • 2
  • 18
  • 25
1

It is probably quicker to split the string based on the item you want and then getting the length of the resulting array.

var haystack = 'file123,file456,file789';
var needle = ',';
var count = haystack.split(needle).length - 1;
haxxxton
  • 6,422
  • 3
  • 27
  • 57
1

Since split has to create another array, I would recommend writing a helper function like this

function count(originalString, character) {
    var result = 0, i;
    for (i = 0; i < originalString.length; i += 1) {
        if (character == originalString.charAt(i)) {
            result +=1;
        }
    }
    return result;
}
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
  • 1
    [It might have to create another array, but it's also very fast compared to this](http://jsperf.com/count-occurences-of-string-in-string). That said, your solution obviously uses less memory. – h2ooooooo Apr 30 '14 at 10:17
  • @h2ooooooo But this is the slowest. :'( – thefourtheye Apr 30 '14 at 10:19
  • 1
    You could have taken advantage of `String.indexOf()` (second part of my answer) ... I guess it all comes down to reducing "userland" code as much as possible .. maybe :) – Ja͢ck Apr 30 '14 at 10:34
  • @Jack Thanks :) As you said, its best to rely on the builtin functions as much as possible it seems – thefourtheye Apr 30 '14 at 10:36
0
var count = "file123,file456,file789".split(",").length - 1;
slash197
  • 9,028
  • 6
  • 41
  • 70