Capitalize the First Letter of a String in Java Using Regex Last Updated : 02 Feb, 2024 Comments Improve Suggest changes Like Article 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. Comment More infoAdvertise with us Next Article Capitalize the First Letter of a String in Java Using Regex devanshuma3v23 Follow Improve Article Tags : Java Java Programs java-regular-expression regular-expression Java Examples +1 More Practice Tags : Java Similar Reads Capitalize the First Letter of a String in Java In Java, to check if a String contains only uppercase letters, we can use many approaches. These approaches verify each character in the String to ensure they are all in uppercase. A simple method, such as iterating through the characters and checking their case. In this article, we will discuss how 4 min read How to Replace the First Occurrence of a String Using Regex in Java? Regex is a very interesting way to search for patterns in a String that the user provides. Regex stands for Regular Expressions. It consists of some patterns that can be planned and modified according to the usage of the program. In this article, we will discuss how to replace the first occurrence o 2 min read Remove first and last character of a string in Java Given a string str, the task is to write the Java Program to remove the first and the last character of the string and print the modified string.Examples:Input: str = "GeeksForGeeks"Output: "eeksForGeek"Explanation: The first and last characters of the given string are 'G' and 's' respectively. Afte 4 min read How to Remove Duplicates from a String in Java Using Regex ? In Java programming, Regex (regular expressions) is a very important mechanism. It can be used to match patterns in strings. Regular expressions provide a short and simple syntax for describing the patterns of the text. In this article, we will be learning how to remove duplicates from a String in J 2 min read How to Remove All Punctuation from a String using Regex in Java? In this article, we will explain how to remove all punctuation from a given String by using Java with the help of Regex. Here regex means regular expression. The regex is a powerful way to explain the search pattern. One more thing is that regular expressions are mostly used for Searching a required 4 min read How to find the first and last character of a string in Java Given a string str, the task is to print the first and the last character of the string. Examples: Input: str = "GeeksForGeeks" Output: First: G Last: s Explanation: The first character of the given string is 'G' and the last character of given string is 's'. Input: str = "Java" Output: First: J Las 3 min read Capitalize the first and last character of each word in a string Given the string, the task is to capitalise the first and last character of each word in a string.Examples: Input: Geeks for geeks Output: GeekS FoR GeekS Input: Geeksforgeeks is best Output: GeeksforgeekS IS BesT Approach Create a character array of the StringRun a loop from the first letter to the 6 min read How to Convert a Java String Against a Pattern Regex? Regular expressions, or "regex," are an effective tool in Java programming for checking texts against a given pattern. This post will walk you through the process of utilizing regular expressions to validate a Java text against a pattern. Prerequisites:String in JavaRegular Expressions in JavaConver 2 min read Java Program to Replace Multiple Characters in a String In this program, we will be discussing various methods for replacing multiple characters in String. This can be done using the methods listed below: Using String.replace() methodUsing replaceAll() method Using replaceFirst() method Method 1: Using String.replace() method This method returns a new st 3 min read Java Program to Search a Particular Word in a String Using Regex In Java string manipulation, searching for specific words is a fundamental task. Regular expressions (regex) offer a powerful and flexible approach to achieve this search. It matches the patterns simply by comparing substrings. In this article, we will learn how to search for a particular word in a 3 min read Like