Java 8 – How to convert first character of every word to Uppercase ?

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
    1. Get the stream from the given String
    2. Split the String based on the space delimiter using split() method
    3. After splitting, convert 1st character of each word/string to uppercase and concatenate remaining letters
    4. 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 :

References :

Happy Coding !!
Happy Learning !!

Java 8 – How to convert duplicate characters to Uppercase in a String ?
Java 8 – How to check whether a number exists in an Arrays or List or Stream ?