Home > Java > javaTutorial > Java program: Capitalize first letter of each word in a string

Java program: Capitalize first letter of each word in a string

王林
Release: 2023-08-20 15:45:13
forward
1316 people have browsed it

Java program: Capitalize first letter of each word in a string

A string is a class of 'java.lang' package that stores a series of characters. Those characters are actually String-type objects. We must enclose the value of string within double quotes . Generally, we can represent characters in lowercase and uppercase in Java. And, it is also possible to convert lowercase characters into uppercase. This article aims to discuss a Java program to convert the first character of each word into uppercase in a string.

Java program to Capitalize the first character of each word in a String

Before making a Java program to convert the first lowercase character of a string into uppercase, let's understand the problem statement first with the help of an example −

Example

Input String

simply easy learning tutorialspoint
Copy after login

Output string

Simply Easy Learning Tutorialspoint
Copy after login

To capitalize the first letter of each word in a string, Java provides a built-in method called 'toUpperCase()', which accepts a lowercase character and returns the corresponding uppercase character.

The Chinese translation of

Example 1

is:

Example 1

The following example demonstrates how we can capitalize the first character of each word in a string.

method

  • First, declare and initialize a String. Then, convert that string into a character array using an in-built method named 'toCharArray()'.

  • Next, take a for loop that will run up to the size of the character array.

  • Inside this for loop, define an if block to check whether the character array contains spaces. If the compiler encounters a space, it converts the next character to uppercase and updates the array.

  • Now, we need to convert the character array back to a string.

  • In the end, print the result and exit.

public class Capitalize {
   public static void main(String[] args) {
      String myinput = "simply easy learning tutorialspoint";
      // store each character to a char array
      char[] charAray = myinput.toCharArray();
      System.out.println("Before capitalizing: " + myinput);
      // for loop to capitalize first letter 
      for(int i = 0; i < charAray.length; i++) {
         // capitalizing first letter of first word
         charAray[0] = Character.toUpperCase(charAray[0]);
         // loop to check if there is space between two letters
         if(charAray[i] == ' ') {
            // capitalizing first letter of rest of the word
            charAray[i+1] = Character.toUpperCase(charAray[i+1]);
         }
      }
      // converting the character array to the string
      myinput = String.valueOf(charAray);
      // to print the final result
      System.out.println("After capitalizing the first letter: " + myinput);
   }
}
Copy after login

Output

Before capitalizing: simply easy learning tutorialspoint
After capitalizing the first letter: Simply Easy Learning Tutorialspoint
Copy after login
Copy after login

Example 2

Translated into Chinese:

Example 2

In the following example, we will use user-defined methods to perform the same tasks and logic.

public class Capitalize {
   public static void Capital(String myinput) { // user-defined method
      // store each character to a char array
      char[] charAray = myinput.toCharArray();
      // for loop to capitalize first letter 
      for(int i = 0; i < charAray.length; i++) {
         // capitalizing first letter of first word
         charAray[0] = Character.toUpperCase(charAray[0]);
         // loop to check if there is space between two letters
         if(charAray[i] == ' ') {
            // capitalizing first letter of rest of the word
            charAray[i+1] = Character.toUpperCase(charAray[i+1]);
         }
      }
      // converting the character array to the string
      myinput = String.valueOf(charAray);
      // to print the final result
      System.out.println("After capitalizing the first letter: " + myinput); 
   }
   public static void main(String[] args) {
      String myinput = "simply easy learning tutorialspoint";
      System.out.println("Before capitalizing: " + myinput);
      Capital(myinput); // calling the method to capitalize
   }
}
Copy after login

Output

Before capitalizing: simply easy learning tutorialspoint
After capitalizing the first letter: Simply Easy Learning Tutorialspoint
Copy after login
Copy after login

in conclusion

In this article, we discussed two methods to capitalize the first letter of each word in a string. However, one thing both methods have in common is the built-in method 'toUpperCase()', which converts lowercase characters to uppercase characters.

The above is the detailed content of Java program: Capitalize first letter of each word in a string. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template