Java Capitalize First Letter Example
In this post, we feature a comprehensive Java Capitalize First Letter Example. We want to write a Java program to convert uppercase first letter java in a sentence and if any character apart from the first one is in Uppercase then convert in into Lowercase.
1. Introduction
Firstly, lets see how to capitalize the first letter of a single word, Example01:
String output = input.substring(0, 1).toUpperCase() + input.substring(1).toLowerCase();
CaptWord.java
public class CaptWord { public static void main(String args[]) { String input = "wOrD"; String output = input.substring(0, 1).toUpperCase() + input.substring(1).toLowerCase(); System.out.println(output); } }
Output:
Word
Now we want to capitalize eache word in a sentence:
Examples:
Input : jAVA Output :Java Input :CODE Output :Code
2. Java Capitalize First Letter – Different Methods
2.1 Method 1
We conver the given string to a char array and conver the first character of each word into upper-case and the rest of its charachters convert into lower-case. See Example02:
Capitalize.java
public class Capitalize { static String convert(String str) { // Create a char array of given String char ch[] = str.toCharArray(); for (int i = 0; i = 'a' && ch[i] = 'A' && ch[i] <= 'Z') // Convert into Lower-Case ch[i] = (char)(ch[i] + 'a' - 'A'); } // Convert the char array to equivalent String String st = new String(ch); return st; } public static void main(String[] args) { String str = "jAVA cOdE GeeKs"; System.out.println(convert(str)); } }
Output:
Java Code Geeks
2.2 Alternative implementations
2.2.1 Example03
Another way of implementing the previous example is shown in Example03:
Capitalize2.java
public class Capitalize2 { public static String upperCaseAllFirst(String value) { char[] array = value.toCharArray(); // Uppercase first letter. array[0] = Character.toUpperCase(array[0]); // Uppercase all letters that follow a whitespace character. for (int i = 1; i < array.length; i++) { if (Character.isWhitespace(array[i - 1])) { array[i] = Character.toUpperCase(array[i]); } } return new String(array); } public static void main(String[] args) { String value = "phone 00 123456789"; String value2 = "name is john"; // Test our code. String result = upperCaseAllFirst(value); System.out.println("value 1: " + value); System.out.println("result 1: " + result); result = upperCaseAllFirst(value2); System.out.println("value 2: " + value2); System.out.println("result 2: " + result); } }
Output:
value 1: phone 00 123456789 result 1: Phone 00 123456789 value 2: name is john result 2: Name Is John
2.2.2 Example04
We can also use split()
method to separate the given sentence into separate words, see Example04:
StringCapital.java
public class StringCapital { public static void main(String[] args) { String str = "welcome to string capital java program"; StringBuilder result = new StringBuilder(str.length()); String words[] = str.split("\\ "); for (int i = 0; i < words.length; i++) { result.append(Character.toUpperCase(words[i].charAt(0))).append(words[i].substring(1)).append(" "); } System.out.println(result); } }
Output:
Welcome To String Capital Java Program
2.2.3 Example05
Lets see another way of implementation in Example05:
CaptAnySent.java
import java.util.Scanner; public class CaptAnySent { public static void main(String[] args){ Scanner in = new Scanner(System.in); System.out.print("Input your Sentence: "); String line = in.nextLine(); String upperCaseLine = ""; Scanner lineScan = new Scanner(line); while(lineScan.hasNext()) { String word = lineScan.next(); upperCaseLine += Character.toUpperCase(word.charAt(0)) + word.substring(1) + " "; } System.out.println(upperCaseLine.trim()); } }
Output:
Input your Sentence: java code geeks students Java Code Geeks Students
2.3 Method 2: Using Java Inbuilt methods toUpperCase()
We can use the toLowerCase()
method to convert the string into lowercase format. Iterate the string, if any space found in previous iteration and the current element is not space then it shows that current letter is the starting of the word so call the toUpperCase()
method to put the first letter of word in uppercase format and append the string in buffer. Below is the implementation of Example06:
InbuiltCapt.java
public class InbuiltCapt { static String capitailize(String str) { StringBuffer buffer = new StringBuffer(); // Declare a character of space // To identify that the next character is the starting of a new word char ch = ' '; for (int i = 0; i < str.length(); i++) { if (ch == ' ' && str.charAt(i) != ' ') buffer.append(Character.toUpperCase(str.charAt(i))); else buffer.append(str.charAt(i)); ch = str.charAt(i); } return buffer.toString().trim(); } public static void main(String args[]) { String s1 = "i aM A tEaChEr"; // Convert that string into lowercase s1 = s1.toLowerCase(); // Call the method to capitalize each word System.out.println(capitailize(s1)); } }
Output:
I Am A Teacher
2.4 Method 3: Using Java Inbuilt methods WordUtils.capitalizeFully(String str)
Inorder to use WordUtils.capitalizeFully(String)
we create a Maven project and add this dependency to its pom.xml
. See Example07:
<dependencies> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.0</version> </dependency> </dependencies>
Now we can use the method as below:
CaptFull.java
import org.apache.commons.lang3.text.WordUtils; public class CaptFull { public static void main(String args[]) { String input = "aNOthEr seNTenCe"; System.out.println(WordUtils.capitalizeFully(input)); } }
Output:
Another Sentence
This was an article about how to create an uppercase first letter in Java.
3. Download the Source Code
You can download the full source code of this example here: Java Capitalize First Letter Example
so what will be the code when I type my last name and it shows it as 1st letter uppercase, the thing is I don’t want it to come separately when I enter and I hope it makes sense