Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String split and count the number of occurrences and also

I have a string

var stringIHave = "Java$$Java$$jQuery$$Java$$jQuery$$Java$$Java$$Java$$Hibernate$$Java$$Java$$Spring$$Instagram$$jQuery$$jQuery$$";

How to get the count of the number of occurrences of each entry, The occurrence I get, is from a JSON like Java = 8 and etc...

like image 821
Chetan Avatar asked Dec 15 '12 15:12

Chetan


People also ask

How do you count the number of occurrences of a string in a string?

The count() method returns the number of occurrences of a substring in the given string.

How do you count a string split in Python?

To split a string and count the results: Use the str. split() method to split the string into a list of strings. Pass the list to the len() function to get the count of items in the list.

How do you count the occurrence of a given character in a string JavaScript?

match() One of the solution can be provided by the match() function, which is used to generate all the occurrences of a string in an array. By counting the array size that returns the number of times the substring present in a string.


3 Answers

First of all you need to split your srting to array:

var keywordsArr = stringIHave.split( '$$' );

then you need to have an object for example to store counts:

var occur = {};

and then just create simple for loop to count all occurrences:

for( var i = 0; i < keywordsArr.length; i++ ) {
    occur[ keywordsArr[ i ] ] = ( occur[ keywordsArr[ i ] ] || 0 ) + 1;
}

now your object occur will have names as keys and count as values.

See jsFiddle demo.

Also as you have at end of your string $$ you maybe will need to remove last item from keywordsArr so just do after split function call:

keywordsArr.pop();

See demo without last element.

So final code will be like:

var stringIHave = "Java$$Java$$jQuery$$Java$$jQuery$$Java$$Java$$Java$$Hibernate$$Java$$Java$$Spring$$Instagram$$jQuery$$jQuery$$",
  keywordsArr = stringIHave.split( '$$' ),
  occur = {};

keywordsArr.pop();

for( var i = 0; i < keywordsArr.length; i++ ) {
    occur[ keywordsArr[ i ] ] = ( occur[ keywordsArr[ i ] ] || 0 ) + 1;
}

for( var key in occur ) {
    document.write( key + ' - ' + occur[key] + '<br/>' );        
} ​
like image 190
antyrat Avatar answered Oct 26 '22 23:10

antyrat


I'd suggest the following:

function stringCount(haystack, needle) {
    if (!needle || !haystack) {
        return false;
    }
    else {
        var words = haystack.split(needle),
            count = {};
        for (var i = 0, len = words.length; i < len; i++) {
            if (count.hasOwnProperty(words[i])) {
                count[words[i]] = parseInt(count[words[i]], 10) + 1;
            }
            else {
                count[words[i]] = 1;
            }
        }
        return count;
    }
}

console.log(stringCount("Java$$Java$$jQuery$$Java$$jQuery$$Java$$Java$$Java$$Hibernate$$Java$$Java$$Spring$$Instagram$$jQuery$$jQuery$$", '$$'));
​

JS Fiddle demo.

References:

  • Object.hasOwnProperty().
  • parseInt().
  • String.split().
like image 45
David Thomas Avatar answered Oct 26 '22 23:10

David Thomas


It's not entirely clear what final objective is. Following creates an object from string that looks like

Object created:

{
 "Java": 8,
 "jQuery": 4,
 "Hibernate": 1,
 "Spring": 1,
 "Instagram": 1
}

JS:

var str = 'Java$$Java$$jQuery$$Java$$jQuery$$Java$$Java$$Java$$Hibernate$$Java$$Java$$Spring$$Instagram$$jQuery$$jQuery$$';
var arr = str.split('$$')
var obj = {};

for (i = 0; i < arr.length; i++) {
    if (arr[i] != '') {
        if (!obj[arr[i]]) {
            obj[arr[i]] = 0;
        }
        obj[arr[i]]++;

    }
}

You can loop over the object to get all values or simply look up one value

var jQueryOccurences= obj['jQuery'];

DEMO: http://jsfiddle.net/25hBV/1/

like image 37
charlietfl Avatar answered Oct 26 '22 23:10

charlietfl