Convert first character of String to lowercase in Java example shows how to make the first character of string lowercase in Java using various approaches.
How to convert the first character of String to lowercase in Java?
You can use any of the below given approaches to make the first character of the string lower case in Java.
1) Convert the first character of String to lower case using the Character class and a char array
Convert the String to a character array, make the first element of an array lowercase and finally create a string from modified char array as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package com.javacodeexamples.stringexamples; public class StringFirstCharLowerCaseExample { public static void main(String[] args) { String str = "First character to lower case"; System.out.println( firstCharToLowerCase(str) ); } private static String firstCharToLowerCase(String str) { if(str == null || str.length() == 0) return ""; if(str.length() == 1) return str.toLowerCase(); char[] chArr = str.toCharArray(); chArr[0] = Character.toLowerCase(chArr[0]); return new String(chArr); } } |
Output
1 |
first character to lower case |
2) Using the substring method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package com.javacodeexamples.stringexamples; public class StringFirstCharLowerCaseExample { public static void main(String[] args) { String str = "Hello world"; System.out.println( firstCharToLowerCase(str) ); } private static String firstCharToLowerCase(String str) { if(str == null || str.length() == 0) return ""; if(str.length() == 1) return str.toLowerCase(); return str.substring(0, 1).toLowerCase() + str.substring(1); } } |
Output
1 |
hello world |
We took the first character of the String, made it lowercase and then joined it with the rest of the String using the substring method.
3) Using the Character class and charAt method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package com.javacodeexamples.stringexamples; public class StringFirstCharLowerCaseExample { public static void main(String[] args) { String str = "Java example"; System.out.println( firstCharToLowerCase(str) ); } private static String firstCharToLowerCase(String str) { if(str == null || str.length() == 0) return ""; if(str.length() == 1) return str.toLowerCase(); return Character.toLowerCase( str.charAt(0) ) + str.substring(1); } } |
Output
1 |
java example |
This approach is similar to the substring
approach. We took the first character of the string using the charAt
method, made it to lowercase using the toLowerCase
method of the Character class and then joined it with the rest of the String.
4) Using the Apache Commons
Finally, if you are using the Apache Commons library, you can use the uncapitalize
method of the StringUtils class to make the first character of string lower case as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.javacodeexamples.stringexamples; import org.apache.commons.lang3.StringUtils; public class StringFirstCharLowerCaseExample { public static void main(String[] args) { String str = "Apache commons rocks!"; System.out.println( StringUtils.uncapitalize(str) ); } } |
Output
1 |
apache commons rocks! |
This example is a part of the String in Java tutorial.
Please let me know your views in the comments section below.