1//You can user the WordUtils class from apache commons-text library
2WordUtils.capitalize(str)
1String str = "java";
2String cap = str.substring(0, 1).toUpperCase() + str.substring(1);
3// cap = "Java"
4
1String str = "java";
2String cap = str.substring(0, 1).toUpperCase() + str.substring(1);
3// cap = "Java"
1class Main {
2 public static void main(String[] args) {
3
4 // create a string
5 String message = "everyone loves java";
6
7 // stores each characters to a char array
8 char[] charArray = message.toCharArray();
9 boolean foundSpace = true;
10
11 for(int i = 0; i < charArray.length; i++) {
12
13 // if the array element is a letter
14 if(Character.isLetter(charArray[i])) {
15
16 // check space is present before the letter
17 if(foundSpace) {
18
19 // change the letter into uppercase
20 charArray[i] = Character.toUpperCase(charArray[i]);
21 foundSpace = false;
22 }
23 }
24
25 else {
26 // if the new character is not character
27 foundSpace = true;
28 }
29 }
30
31 // convert the char array to the string
32 message = String.valueOf(charArray);
33 System.out.println("Message: " + message);
34 }
35}
1import org.apache.commons.text.WordUtils;
2
3public class WordUtilsCapitalizeFullyExample {
4 public static void main(String[] args) {
5 String result1 = WordUtils.capitalizeFully("how to CAPITALIZE first letter words");
6 System.out.println(result1);
7 }
8}