Java 8 – How to count length of last word in a String ?

In this article, we will learn how to count length of last word in a String using String‘s length() method

Count length of Last word in a String :

  • Using Java 1.7 version
  • Using Java 8 Stream

1. Count length of Last word using Java 1.7 version :

  • First step is to split the given String based on space delimiter which returns String[] Arrays
  • Then use length() method of String for getting length of last word as shown in the below illustration
  • Finally, print original String & length of last word to the console

CountLengthOfLastWordInString.java

package in.bench.resources.java8.string.methods;

public class CountLengthOfLastWordInString {

	public static void main(String[] args) {

		// 1. test string with commas
		String str = "Java is a powerful language";


		// 1.1 print to console
		System.out.println("Original String :- \n" + str);


		// 2. split String based on space delimiter
		String[] strArray = str.split("\\s");


		// 2.1 length of last word
		int length = strArray[strArray.length-1].length();


		// 2.1 print to console
		System.out.print("\nLength of last word '" 
				+ strArray[strArray.length-1] 
						+ "' is :- \n" 
						+ length);
	}
}

Output:

Original String :- 
Java is a powerful language

Length of last word 'language' is :- 
8

2. Count length of Last word using Java 8 Stream :

  • A List contains Strings from which length of last word needs to be obtained using Stream & String‘s length() method
    • Get stream for the list
    • Convert each String in the List into String[] Arrays by splitting on the basis of space delimiter using Stream.map() method
    • Then get last word from the split-ed String and find its length using length() method of String
    • Finally, print last word of the String and its length using Stream.peek() & Stream.forEach() methods respectively to the console

CountLengthOfLastWordInStringUsingJava8.java

package in.bench.resources.java8.string.methods;

import java.util.ArrayList;
import java.util.List;

public class CountLengthOfLastWordInStringUsingJava8 {

	public static void main(String[] args) {

		// 1. List of Strings
		List<String> stringsWithCommas = new ArrayList<>();


		// 1.1 add strings to List
		stringsWithCommas.add("Improve your Habits");
		stringsWithCommas.add("I have no idea");
		stringsWithCommas.add("The matter is clear");
		stringsWithCommas.add("You are a Glutton");
		stringsWithCommas.add("It's up to you");


		// 1.2 print to console
		System.out.println("Original List of String :- \n");
		stringsWithCommas.forEach(str -> System.out.println(str));


		// 2. count length of last word using String.length() method
		System.out.println("\n\nLength of last word :- \n");
		stringsWithCommas
		.stream() // get stream
		.map(str -> str.split("\\s")) // split & convert to String[]
		.peek(strArr -> System.out.print(strArr[strArr.length-1]))
		.map(strArr -> strArr[strArr.length-1].length()) // get length of last word
		.forEach(strLength -> System.out.println("\t\t" + strLength)); // print to console
	}
}

Output:

Original List of String :- 

Improve your Habits
I have no idea
The matter is clear
You are a Glutton
It's up to you


Length of last word :- 

Habits		6
idea		4
clear		5
Glutton		7
you		3

Related Articles :

References :

Happy Coding !!
Happy Learning !!

Java 8 – How to generate Fibonacci numbers using Stream ?
Java 8 - How to remove last comma (,) from String ?