String类的转换功能(方法)

package cn.lgt.stringmethods;

public class StringShiftMethods {
    public static void stringShiftMethods(){
        //String类的转换功能
        //1. 把字符串转换为字节数组
        //byte[] getBytes();
        String s1 = "abcde";
        byte[] byt = s1.getBytes();
        for(int i = 0; i < byt.length; i++){
            System.out.print(byt[i] + "  ");
        }
        System.out.println();
        //2. 把字符串转换为字符数组
        //char[] toCharArray();
        char[] chr = s1.toCharArray();
        for(int i = 0; i <chr.length; i++){
            System.out.print(chr[i] + "  ");
        }
        System.out.println();
        //3. 把字符数组转换为字符串
        //static String valueOf(char[] chs);
        char[] chs = {'h','e','l','l','o'};
        //静态方法,以静态方式调用
        String s2= String.valueOf(chs);
        System.out.println(s2);
        //4. 把字符串转换为小写
        //String toLowerCase();
        String s3 = "HELLOJAVA";
        String s4 = s3.toLowerCase();
        System.out.println("s3 = " + s3);
        System.out.println("s4 = " + s4);
        //5. 把字符串转换为大写
        String s5 = "helloWorld";
        String s6 = s5.toUpperCase();
        System.out.println("s5 = " + s5);
        System.out.println("s6 = " + s6);
        //6. 把字符串拼接在一起
        //String concat(String str);
        String s7 = "hello";
        String s8 = "java";
        String s9 = s7.concat(s8);
        System.out.println(s9);
        System.out.println(s9.concat(s9));
    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值