Home > Enterprise >  How can i count a specific words in a certain string?
How can i count a specific words in a certain string?

Time:02-28

i'm new to javascript langguage and still learning some of the data types and i get execise which i can not comprehend, please help me.

The exercise question :

'Love is the best thing in this world. Some found their love and some are still looking for their love.' Count the number of word love in this sentence.

so i tried some of the basic method such as,

  1. match
  2. search
  3. split

and i cannot count the words love and given love words index.

how can i code this to find the answers?

CodePudding user response:

This should work.

let sentence = "Love is the best thing in this world. 
Some found their love and some are still looking for their love."

console.log(sentence.split("love").length)

CodePudding user response:

To make it a little more generic and to show of the use of reduce:

const data = "Love is the best thing in this world. Some found their love and some are still looking for their love"

const dataArray = data.split(' ');

const wordMap = dataArray.reduce((result, word) => {
    result[word.toLowerCase() ]= result[word.toLowerCase() ] ? result[word]   1 : 1;
    return result
}, {});

console.log(wordMap["love"])
console.log(wordMap["is"])
console.log(wordMap["best"])

CodePudding user response:

you can use regex, also you can implement generic regex rules too.

const count = (str) => {
  const re = /love/gmi
  return ((str).match(re) || []).length
}

console.log(count('Love is the best thing in this world. Some found their love and some are still looking for their love'))

CodePudding user response:

This should work

const sentence = 'Love is the best thing in this world. Some found their love and some are still looking for their love.'
const occurrences = sentence.match(/love/gi); // (g is is for match all occurrences, i is for case insensitive)
console.log("number of occurrences of 'love'", occurrences.length)

CodePudding user response:

i have created a solution in Java in eclipse. I am not that programming genius but have a little experience for three years now.

public class Question {

public static void main(String[] args) {
    // TODO Auto-generated method stub
     String s = "Love is the best thing in this world. Some found their love and some are still looking for their love."; 
     String split[]=s.split(" "); //splits every char sequence to an array by space -> " "
    
     int i=0; //counting variable to run through the foor loop
     int count = 0;  //counting the matching char sequences

     for(i=0; i<split.length; i  ) { //running the for loop from o until the end of the array length with the splitted strings
         System.out.println(split[i]); //helping output to see whats been splitted
         if(split[i].contains("Love")|| split[i].contains("love")) { // checking every array space if it contains the word Love or love (case sensitiv)
             count  ; //if it matches count up by one
         }
     }
     System.out.println(count);
}

}

the added comments should explain what i have done in the task. Just look for the output in the console that should clear things up.

The words are the content of the splittet string. The last output (3) is the counted amount of love

Regards Yannik

  • Related