/** * @author lautturi.com * Java example: java count word in string */ import java.util.*; public class Lautturi { public static void main(String[] args) { String str = "hello lautturi java world!"; int words = str.split(" ").length; System.out.println("The number of words in the string :" + words); } }
output:
The number of words in the string :4