day14 JavaSe之String类的转换功能

String类的转换功能:
byte[] getBytes()
char[] toCharArray()
static String valueOf(char[] chs)
static String valueOf(int i)
String toLowerCase()
String toUpperCase()
String concat(String str)
重点
char[] toCharArray()
String concat(String str)

在这里插入图片描述

public class StringDemo8 {
    public static void main(String[] args) {
        String s = "HelloWorLD";

        //public byte[] getBytes()
        // 使用平台的默认字符集将此String编码为字节序列,将结果存储到新的字节数组中。
        byte[] b1 = s.getBytes();
//        System.out.println(b1); //[B@4554617c 数组没有重写toString()方法 打印的是地址值
        for(int i=0;i<b1.length;i++){
            System.out.println(b1[i]);
        }

        System.out.println("*********************");
        //char[] toCharArray()将此字符串转换为新的字符数组。
        //  字符串 ---> 字符数组
        char[] chars = s.toCharArray();
        for(int i = 0;i<chars.length;i++){
            System.out.print(chars[i]);
        }
        System.out.println();

        //增强for循环
//        for (char aChar : chars) {
//            System.out.println(aChar);
//        }

        System.out.println("*********************");
        //字符数组 ----> 字符串
        //static String valueOf(char[] chs)
        System.out.println(chars);

        System.out.println("*********************");
        //static String valueOf(int i)
        //将int类型的数据转化成字符串类型
        String s1 = String.valueOf(100); //  "100"   --> 100
        System.out.println(s1);

        System.out.println("*********************");
        //String toLowerCase()
        //将字符串内容全部转化成小写
        String s2 = s.toLowerCase(); //HelloWorLD
        System.out.println(s2); //helloworld

        System.out.println("*********************");
        // String toUpperCase()
        //将字符串内容全部转大写
        String s3 = s.toUpperCase();//HelloWorLD
        System.out.println(s3);//HELLOWORLD

        System.out.println("*********************");
        //public String concat(String str)将指定的字符串连接到该字符串的末尾。
        //将小括号里面的字符串拼接到调用方法的字符串后面

        String s4 = "Hello";
        String s5 = "World";
        String s6 = s4+s5;
        String s7 = s5.concat(s6);

        System.out.println("s4: "+s4); //Hello
        System.out.println("s5: "+s5); //World
        System.out.println("s6: "+s6); //HelloWorld
        System.out.println("s7: "+s7); //WorldHelloWorld

//        String s8 = "";
        String s8 = null;
//        System.out.println(s8.concat(s5));//NullPointerException
//        System.out.println(s5.concat(s8));//NullPointerException


    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值