string count java code example
Example 1: 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.");
}
Example 2: java count
public static void main(String[] args)
{
// creating a list of Integers
List<Integer> list = Arrays.asList(0, 2, 4, 6,
8, 10, 12);
// Using count() to count the number
// of elements in the stream and
// storing the result in a variable.
long total = list.stream().count();
// Displaying the number of elements
System.out.println(total);
}