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 :
- Java – Remove duplicate characters from String in Java
- Java – How to UPPERCASE every duplicate character/occurrences in a String in Java
- Java – How to check whether 2 strings are Anagrams in Java ?
- Java – How to remove white-spaces in a String ?
- Java – How to swap 2 Strings without using Third variable ?
- Java – Count and print number of Vowels and Consonants in a String
- Java – Remove input-ted Character from a given String
- Java – How to check whether given String contains only Alphabets or Letters ?
- Java – How to check whether given String contains only Digits ?
- Java – How to check whether given String contains Alpha-numeric characters only ?
- Java – How to remove special characters from String ?
- Java – How to remove last comma (,) from String ?
- Java – How to count length of last word in a String ?
References :
- https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
- https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html
Happy Coding !!
Happy Learning !!