Java轉換字串首字母大小寫
阿新 • • 發佈:2019-01-10
package com.hezhikeji.controller;
public class Test {
//首字母轉小寫
public static String toLowerCaseFirstOne(String s)
{
if(Character.isLowerCase(s.charAt(0)))
return s;
else
return (new StringBuilder()).append(Character.toLowerCase(s.charAt(0))).append(s.substring(1 )).toString();
}
//首字母轉大寫
public static String toUpperCaseFirstOne(String s)
{
if(Character.isUpperCase(s.charAt(0)))
return s;
else
return (new StringBuilder()).append(Character.toUpperCase(s.charAt(0))).append(s.substring(1)).toString();
}
public static void main(String[] args) {
//輸出test
System.out.println(toLowerCaseFirstOne("test"));
//輸出test
System.out.println(toLowerCaseFirstOne("Test"));
//輸出Test
System.out.println(toUpperCaseFirstOne("test"));
//輸出Test
System.out.println(toUpperCaseFirstOne("Test" ));
}
}