Capitalize first letter of each word Java
In this post, you will learn how to capitalize the first letter of each word in Java. In the development process, there are different situations where we need to make a string of completely uppercase or lowercase or camel case letters. There are different ways to capitalize the first letter of each using Java. Here, we have mentioned most of them -
Method 1
We can without much of a stretch capitalize the first letter of each word of a sentence by splitting the sentence into words using the split() method and applying the toUpperCase() on each word. The split() method of java splits the string against a given regular expression and returns a char array. The toUpperCase() method of java converts a string to upper case letters. The given example demonstrates how to capitalize the first letter of each word of a sentence.
public class CapitalizeWord
{
public static void main(String[] args) {
String sentence = "success is journey not a destination";
//Split sentence into words
String words[]=sentence.split("\\s");
String capitalizeWord ="";
for(String w: words){
// getting first letter
String first = w.substring(0,1);
// getting rest of the letters
String rest = w.substring(1);
//Capitalizing the first letter of each word and
// concatenate them
capitalizeWord+=first.toUpperCase()+ rest+ " ";
}
// remove extra space at the end before returning
System.out.println(capitalizeWord.trim());
}
}
Output of the above code:
Success Is Journey Not A Destination
Method 2
In the given example, we have capitalized the first letter of the string using a for loop and an if else statement. In this, first we have converted the given string into a char array and accessed every element of the char array. If the element is white space, we convert the next element into uppercase.
public class capitalizeWord {
public static void main(String[] args) {
// declaring a sentence
String sentence = "The learning process continues until the day you die";
// storing each characters to a char array
char[] charArray = sentence.toCharArray();
boolean space = true;
for(int i = 0; i < charArray.length; i++) {
if(Character.isLetter(charArray[i])) {
// check presence of space before the letter
if(space) {
// change the letter into uppercase
charArray[i] = Character.toUpperCase(charArray[i]);
space = false;
}
}
else {
// if the new character is not character
space = true;
}
}
// converting the char array to the string
sentence = String.valueOf(charArray);
System.out.println("Sentence: " + sentence);
}
}
Output of the above code:
Sentence: The Learning Process Continues Until The Day You Die
Related Articles
Convert binary to decimal in JavaConvert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java
Check whether the given number is even or odd in java
Print prime numbers from 1 to 100 in Java
Java prime number program
Java program to convert celsius to fahrenheit
Fibonacci series program in Java
Java program to check leap year
Java program to find factorial of a number