Answers for "find number of words in string in java using split"

1

how to count number of words in a string

String name = "Carmen is a fantastic play"; //arbitrary sentence
        
        int numWords = (name.split("\\s+")).length; //split string based on whitespace
                                                //split returns array - find legth of array
        
        System.out.println(numWords);
Posted by: Guest on June-06-2020

Code answers related to "find number of words in string in java using split"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language