取字符串首字母变更为大写字母,其余小写字母(两种方法 substring()与charAt())。

package demo_取部分字符串将其转换大小写;

public class toUpperCaseAndtoLowerCase {
    public static void main(String[] args) {
        
        toConvert("sdfdsgre");
    }
    /**
     * 方法1  使用substring方法取字符
     */
    public static void function(String str){
        
        String first = str.substring(0,1); //取首字符
        String after = str.substring(1);   //取剩余字符
        first = first.toUpperCase();       //首字符变为大写
        after = after.toLowerCase();       //剩余字符变为小写
        System.out.println(first+after);
        
            
    }
    /**
     *   方法2 charAt方法     
     *   character.toUpperCase(str.charAt(0)); 
     */
    
    public static void toConvert(String str){
        char array = Character.toUpperCase(str.charAt(0));
        //charAt方法取首字符
        String S1 = str.substring(1);  //剩余字符
        S1 = S1.toLowerCase();
        System.out.println(array+S1);
        
    }
}

猜你喜欢

转载自www.cnblogs.com/bigmaxblog/p/10020746.html
今日推荐