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.


import_contacts

You may also like:

Related Posts

  • Count the Number of Occurrences of a Character in a String in Java
    05 April

    Count occurrences of Character in String in Java

    Table of Contents1. Using String Library Methods2. Using Recursion3. Using Hashing ConceptUsing ArraysUsing Collections (Map) 4. Using Java 8 Features In this article, we will look at a problem: Given an Input String and a Character, we have to Count Occurrences Of character in String in java. For Example, If the Given String is : […]

  • 09 October

    Java program to find first and last digit of a number

    Table of ContentsAlgorithmUsing while loopUsing log() and pow() methodsUsing while loop and pow() method In this article, we are going to find first and last digit of a number in Java. To find first and last digit of any number, we can have several ways like using modulo operator or pow() and log() methods of […]

  • 03 October

    Happy Number program in Java

    Table of ContentsWhat is a Happy Number?Using HashsetAlgorithmExampleUsing slow and fast pointersAlgorithmExample In this article, we are going to learn to find Happy Number using Java. Let’s first understand, what is Happy Number? What is a Happy Number? A number which leaves 1 as a result after a sequence of steps and in each step […]

  • 30 September

    Find Perfect Number in Java

    Table of ContentsIterative approachRecursive approach In this article, we are going to find whether a number is perfect or not using Java. A number is called a perfect number if the sum of its divisors is equal to the number. The sum of divisors excludes the number. There may be several approaches to find the […]

  • 28 September

    How to find Magic Number in Java

    Table of ContentsWhat is a Magic Number?Algorithm for Magic NumberExample to find Magic NumberAnother Example To find Magic Number In this article, we are going to learn to find Magic Number using Java. Let’s first understand, what is Magic Number? What is a Magic Number? A number which leaves 1 as a result after a […]

  • 28 September

    Number guessing game in java

    Table of ContentsNumber guessing game RulesAlgorithm for Number guessing game In this article, we will implement Number guessing game in java. The number guessing game is based on a concept where player guesses a number between a range. If player guesses the exact number then player wins else player looses the game. Since this game […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.