how to modify string’s case

Save & Share

 

This example shows how to change case of whole string:

 

   public void testModifyCase (){
 
        String lowerCaseString = "how to modify string case?";
 
        String upperCaseString = lowerCaseString.toUpperCase();
        System.out.println(upperCaseString);
        //output: HOW TO MODIFY STRING CASE?
 
        lowerCaseString = null;
        lowerCaseString = upperCaseString.toLowerCase();
        System.out.println(lowerCaseString);
        //output: how to modify string case?
 
    }

 

 

And this one just capitalize first letter:

 

   public void testCapitalizeFirstLetter(){
        String lowerCaseString = "how to capitalize first letter?";
 
        StringBuilder builder = new StringBuilder();
 
        builder.append(Character.toUpperCase(lowerCaseString.charAt(0))).append(lowerCaseString.substring(1));
 
        String stringWithCapitalizedFirstLetter = builder.toString();
        System.out.println(stringWithCapitalizedFirstLetter);
        //output: How to capitalize first letter?
 
    }
Top Of Page