练习关于String的一系列文件

public class TestString2 {
    public static void main(String[] args) {
        String s = "abc";
        char[] value = {'a','b','c','d'};
        String ss = new String(value);

        System.out.println(s.charAt(1));//b,获取指定数组下标的值,注意下标从0开始
        System.out.println(s.concat("cxy"));//abccxy
        System.out.println(s);//abc
        String s3 = s.concat("cxy");//要使用拼接效果,交给变量保存,可以多次使用
        System.out.println(s3);
        System.out.println(s.startsWith("a"));//ture,判断本串是否以指定字母开头
        System.out.println(s.endsWith("a"));//false,判断本串是否以指定字母结尾
        System.out.println(s==ss);//false,比较地址值
        System.out.println(s.equals(ss));//false,String重写了equals(),比较的是串的具体数值
        System.out.println("上面是我要的答案");
        System.out.println(s.length());//3,得出本串的长度
        String s4 = "abcbca";
        System.out.println(s4.indexOf("c"));//2,判断当前指定的字符在本串第一次出现的下标
        System.out.println(s4.lastIndexOf("c"));//4,判断当前字符在本串最后一次出现的下标
        System.out.println(s4.toLowerCase());//abcbca,把当前串转为全小写
        System.out.println(s4.toUpperCase());//ABCBCA,把当前串转为全大写
        System.out.println(s.getBytes());//[B@4554617c,把指定串转成字节数组,返回值是byte[]
        System.out.println(Arrays.toString(s.getBytes()));//[97, 98, 99]
        String s5 = "a b c d e";
        System.out.println(s5.split(" "));//[Ljava.lang.String;@74a14482
        System.out.println(Arrays.toString(s5.split(" ")));//[a, b, c, d, e]
        System.out.println(s5.substring(3)); //c d e,截取,从指定下标处开始截取子串
        System.out.println(s5.substring(1,5));// b c,截取子串,含头不含尾
        s5 = "       abcde            ";
        System.out.println(s5.trim());//本方法用于去除首尾两端的空格
        System.out.println(String.valueOf(10));//10,把int类型转换成String类型
        System.out.println("20"+10);//2010,两个数据类型进行拼接
        System.out.println(String.valueOf(10)+10);//1010,int10转换成了String10后进行拼接
        System.out.println(10+10);//20,可以计算

    }
}
/*本类用于练习String常用方法*/
public class TestString3 {
    public static void main(String[] args) {
        char[] value = {'a','b','c','d','e'};
        //1.length()-查看字符串的长度
        String ss = new String(value);
        String s = "abcd";
        System.out.println(s.length());
        System.out.println(value.length);
        //2.charAt()—获取指定下标处位置上的字符
        System.out.println(s.charAt(2));
        System.out.println(ss.charAt(3));
        //3.lastIndexOf()-某个字符最后一次出现的位置
        System.out.println(s.lastIndexOf("b"));
        System.out.println(ss.lastIndexOf("b"));
        //4.substring()-截取子串,如果参数有两个左闭右开[1,5)
        System.out.println(s.substring(1,3));
        System.out.println(ss.substring(0,3));
        //5.equals()-判断两个串是否相等,注意String重写了Object的此方法,所以内容相同就返回true
        System.out.println(s==ss);
        System.out.println(s.equals(ss));
        //6.startsWith()-判断是不是以参数开头
        System.out.println(s.startsWith("a"));
        System.out.println(ss.startsWith("a"));
        //7.endsWith()–判断是不是以参数结尾
        System.out.println(s.endsWith("e"));
        System.out.println(ss.endsWith("e"));
        //8.split()—以指定字符分割
        System.out.println(s.split("c"));
        System.out.println(ss.split("c"));
        System.out.println(Arrays.toString(s.split("c")));
        System.out.println(Arrays.toString(ss.split("c")));
        //9.trim()-去掉首尾两端的空格
        String t = "   abcd    ";
        System.out.println(t.trim());
        //10.getBytes()-把串转换成数组
        System.out.println(s.getBytes());
        System.out.println(Arrays.toString(s.getBytes()));
        //11.toUpperCase()-变成全大写
        System.out.println(s.toUpperCase());
        System.out.println(ss.toUpperCase());
        //12.toLowerCase()-变成全小写
        String e = "ABCDEFG";
        System.out.println(e.toLowerCase());
        //13.String.valueOf(10)-把int类型的10转换成String类型
        System.out.println(String.valueOf(20)+10);
        //14.拼接
        System.out.println(s.concat("find"));
        System.out.println(ss.concat("find"));
        //15.判断本字符出现的第一个下标
        System.out.println(s.indexOf("c"));
        System.out.println(ss.indexOf("c"));
        System.out.println(e.indexOf("D"));

    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值