Answers for ". Java program to find the number of words in String ?"

3

count the number of words in a string java

public static void main(String[] args)
    { 
        //return the number of words in a string 
        
       String example = "This is a good exercise"; 
       
       int length = example.split(" ").length;
       
       System.out.println("The string is " + length + " words long.");
        
        
    }
Posted by: Guest on June-04-2020
0

check how many of a word is in a string

function countOccurences(str,word)  
{ 
    // split the string by spaces in a 
    String a[] = str.split(","); 
  
    // search for pattern in a 
    int count = 0; 
    for (int i = 0; i < a.length; i++)  
    { 
    // if match found increase count 
    if (word.equals(a[i])) 
        count++; 
    } 
  
    return count; 
}
Posted by: Guest on November-16-2020

Code answers related to ". Java program to find the number of words in String ?"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language