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"
1String str = "hello world!";
2
3// capitalize first letter
4String output = str.substring(0, 1).toUpperCase() + str.substring(1);
5
6// print the string
7System.out.println(output);
8// Hello world!
9
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}
1String str = "java";
2String cap = str.substring(0, 1).toUpperCase() + str.substring(1);