Manera de contar palabras en una cadena

I just need a function for counting a given word in a string. There is something that has to do with split but I cant get what I should do from then. Thank u for your time.

PS I forgot to mention. My apologies. I want to do that with javascript.

preguntado el 08 de noviembre de 11 a las 14:11

In what programming language do you want to do this? -

Lets say this string: "dog, cat, horse, cat, bird, cat, horse, bird, dog" How many times each animal appears in that string? -

just look at to the link i have provided.Its exactly that you need -

Thank u all for yr answers. I think bilash.saha provided me the code I need. Im going to check it. -

2 Respuestas

After splitting by the word, just take the length of the resulting array and subtract one.

What programming language are you doing this in?

respondido 08 nov., 11:18

I think its counting 'a given word' in a string , not the number of word in the string. - bilash.saha

@bilash.saha: Missed the dado. Corregido. - Dennis

As I don't know in which programming language you want to do this.

Here is a nice example of Word counting with frequency of occurrence in Javascript.

You can take a look what its doing by clicking 'Try this example' button.

There is word count, replace and capitalize.You just need the wordCount() function.

respondido 08 nov., 11:18

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.