In this article, we will discuss how to convert first character of every word to uppercase using Java 1.8 version
In one of the previous article, we already discussed about converting first character of every word in a String using Java version below 8, read Java – Convert first character of every word to uppercase
Convert 1st character of every word to Uppercase :
- Steps to convert 1st character of every word to Uppercase –
- Get the stream from the given String
- Split the String based on the space delimiter using split() method
- After splitting, convert 1st character of each word/string to uppercase and concatenate remaining letters
- And then collect by joining converted string with space delimiter
- Finally, print both original/converted String to the console
ConvertFirstCharacterOfEveryWordToUpperCase.java
package in.bench.resources.java8.string.methods;
import java.util.Arrays;
import java.util.stream.Collectors;
public class ConvertFirstCharacterOfEveryWordToUpperCase {
public static void main(String[] args) {
// 1. sample string
String str = "this world has very good leader"
+ " only that they need to be identified";
// 1.1 print to console
System.out.println("Original String :- \n" + str);
// 2. convert 1st char to upper case
str = Arrays
.stream(str.split("\\s"))
.map(word -> Character.toUpperCase(word.charAt(0)) + word.substring(1))
.collect(Collectors.joining(" "));
// 2.1 print to console
System.out.print("\nUppercase converted String :- \n" + str);
}
}
Output:
Original String :-
this world has very good leader only that they need to be identified
Uppercase converted String :-
This World Has Very Good Leader Only That They Need To Be Identified
Related Articles :
- Java 8 – Count and print number of lines and words in a text file
- Java 8 – Count and print number of repeated word occurrences in a text file
- Java 8 – Count and print number of repeated character occurrences in a String
- Java 8 – Count and print number of Vowels and Consonants in a String
- Java 8 – Reverse each words in a String using Stream and Collectors
- Java 8 – Reverse complete/entire String using Stream and Collectors
- Java 8 – Remove input-ted Character from a given String
- Java 8 – How to split a String and Collect to any Collection ?
- Java 8 – How to check whether given String contains only Alphabets or Letters ?
- Java 8 – How to check whether given String contains only Digits ?
- Java 8 – How to check whether given String contains Alphanumeric characters only ?
- Java 8 – How to convert first character of every word to Uppercase ?
- Java 8 – How to convert duplicate characters to Uppercase in a String ?
- Java 8 – How to remove special characters from String ?
References :
- https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
Happy Coding !!
Happy Learning !!