Capitalize the First Letter of a String in Java Using Regex Last Updated : 23 Jul, 2025 Comments Improve Suggest changes 1 Likes Like Report In Java, we can use regular expressions to make the first character of each word in Uppercase as the word is represented as a String and string is the collection of characters. So, in Java, we can use pattern and matcher class methods to find the first character of a word and then replace it with its uppercase letter. In this article, we will discuss how to make the first character of each word in Uppercase using regex. Example of Converting the first character of each word in UppercaseInput: geeks for geeksOutput: Geeks For Geeks Input: hello worldOutput: Hello World Program to Make the First Character of Each Word in Uppercase Using RegexBelow is the Regular Expression to make the first character of each word in Uppercase. Java // Java Program to make the first character of each word in Uppercase using regex import java.io.*; import java.util.regex.Matcher; import java.util.regex.Pattern; class GFG { public static void main(String[] args) { // given input string String s1 = "geeks for geeks"; // regular expression for finding the first letter // of string Pattern pat = Pattern.compile("\\b\\w"); Matcher Match = pat.matcher(s1); StringBuilder answer = new StringBuilder(); // replace the first character with it's uppercase while (Match.find()) { Match.appendReplacement( answer, Match.group().toUpperCase()); } Match.appendTail(answer); // print the output on the console System.out.println("" + answer); } } OutputGeeks For Geeks Explanation:In the above program, it converted the first character of each word to uppercase in a given input string using regex. It uses a regular expression (\\b\\w) to identify the first letter of each word. The program then iterates through the matches, replacing each first letter with its uppercase equivalent. Finally, it prints the modified string. In simple, it transforms a sentence so that the first letter of each word is in uppercase. Create Quiz Comment D devanshuma3v23 Follow 1 Improve D devanshuma3v23 Follow 1 Improve Article Tags : Java Java Programs java-regular-expression regular-expression Java Examples +1 More Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings7 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java9 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface4 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like