Convert String to Initcap Using Java

 Posted On  | Yashwant Chavan 

Sample code convert input string into init cap letter using java,First we convert the input string into char array. Then convert its first letter to Upper Case.

toInitCap() method

package com.technicalkeeda.app;

public class InitCapExample {
    public static void main(String[] args) {
        System.out.println(toInitCap("yashwant"));
        System.out.println(toInitCap("mahesh"));
    }

    public static String toInitCap(String param) {
        if (param != null && param.length() > 0) {
            char[] charArray = param.toCharArray();
            charArray[0] = Character.toUpperCase(charArray[0]);
            // set capital letter to first position
            return new String(charArray);
            // return desired output
        } else {
            return "";
        }
    }
}

Output

Yashwant
Mahesh


© technicalkeeda.com 2017

 |  Find us on Google+ |  Rss Feed

Loaded in 0.0332 seconds.