java 字符串方法总结

public class method_learn {
    public static void main(String[] args) {
        String s = "zhangsan";
        String s1 = "sb";
        //尾接
        String s2 = s.concat(s1);
        System.out.println(s2);

        //比较
        System.out.println(s.equals(s1));

        //返回指定索引的字符
        System.out.println(s.charAt(0));
        String s3 = "sa";

        //返回子字符串首次出现的索引
        System.out.println(s.indexOf(s1));
        System.out.println(s.indexOf(s3));

        //从beginIndex截取字符串
        System.out.println(s.substring(3));

        //截取beginindex和endindex之间的字符串,规则是左闭右开
        System.out.println(s.substring(0,5));
        System.out.println(s.substring(3,5));

        //转换为字符数组 alt=enter可以 创建对象
        char[] chars = s1.toCharArray();
        System.out.println(chars);

        //字符数组转化为字符串
        String s4 = new String(chars);
        System.out.println(s4);

        //使用平台编码  得到byte数组
        byte[] bytes = s3.getBytes();
        System.out.println(bytes);//[B@1b6d3586

        //根据byte数组 得到对应的字符编码下的字符串
        String s5 = new String(bytes);
        System.out.println(s5);//sa

        //使用replacement代替target子字符串
        String s6 = s5.replace("a", "niubi");
        System.out.println(s6);

        //按照指定规则分割 得到字符串数组,由于|和.都是转义字符,所以都需要添加

        String s7 = "aa|bb|cc";
        String[] strArray = s7.split("\\|"); // ["aa","bb","cc"]
        for(int x = 0; x < strArray.length; x++) {
            System.out.println(strArray[x]); // aa bb cc
        }

        //使用shift+f6可以统一重命名 正则表达式是正向跑的,和注释是方向相反的
        String s8 = "www.baidu.com";
        String[] split = s8.split("\\.");
        System.out.println(split.length);

        for(int i=0;i< split.length;i++){
            System.out.println(split[i]);
        }


    }

}

总结:

  • contact 尾接 
  • equals & == 比较 一种是地址比较;一种是数值比较
  • charAt(index) 返回指定索引的字符
  • indexof(s1) 返回子字符串出现的index
  • substring(begin,end) 截取子字符串
  • toCharArray() 返回字符数组
  • getBytes() 返回字节数组
  • String(char [] or byte []) 返回字符串
  • replace(target,newString) 替换子字符串
  • split(pattern) 正则表达式分割 正则 需要加转义字符

IDEA快捷键:

  • shift+ctrl+F10 run操作
  • alt+enter 创建new的实例变量
  • shift+F6 统一重命名后面的变量名称
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值