A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/javaexamples/regular_case.htm below:

How to make first character of each word in Uppercase using Java

How to make first character of each word in Uppercase using Java Problem Description

How to make first character of each word in Uppercase?

Solution

Following example demonstrates how to convert first letter of each word in a string into an uppercase letter Using toUpperCase(), appendTail() methods.

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
   public static void main(String[] args) {
      String str = "this is a java test";
      System.out.println(str);
      StringBuffer stringbf = new StringBuffer();
      Matcher m = Pattern.compile(
         "([a-z])([a-z]*)", Pattern.CASE_INSENSITIVE).matcher(str);
      
      while (m.find()) {
         m.appendReplacement(
            stringbf, m.group(1).toUpperCase() + m.group(2).toLowerCase());
      }
      System.out.println(m.appendTail(stringbf).toString());
   }
}
Result

The above code sample will produce the following result.

this is a java test
This Is A Java Test

java_regular_exp.htm


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4