How to capitalize first letter in java

In this post, we will how to capitalize first letter in java

Capitalize first letter of String

Here are the steps to convert first letter of string to uppercase in java

  • Get first letter of String firstLetStr using str.substring(0,1).
  • Get remaining String remLetStr using str.substring(1).
  • Convert first letter of String firstLetStr to upper Case using toUpperCase() method.
  • Concatenate both the String firstLetStr and remLetStr.

Output:

Original String: java2blog
String with first letter as Capital: Java2blog

Capitalize first letter of each word

Here are the steps to capitalize first letter of each word.

  • Split String by space and assign it String array words
  • Iterate over the String array words and do following:
    • Get first letter of String firstLetter using str.substring(0,1).
    • Get remaining String remainingLetters using str.substring(1).
    • Convert first letter of String firstLetter to upper Case using toUpperCase() method.
    • Concatenate both the String firstLetter and remainingLetters.

Output:

This Is Java Code

That’s all about How to capitalize first letter in java.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *