【Java 字符串常用方法】

public class StringTest {
    public static void main(String[] args) {
        String s=" ling zhi i love You ";
        //1、返回字符第一次出现的位置,未在字符里返回-1
        int index=s.indexOf('#');
        System.out.println(index);//-1
        //2、用后面的新字符替换前面老的字符
        String replace_s=s.replace('l','s');
        System.out.println(s); // ling zhi i love You  //
        System.out.println(replace_s);// sing zhi i sove You //
        //3、去除字符串左右的空格
        String trim_s=s.trim();
        System.out.println(s);// ling zhi i love You  //
        System.out.println(trim_s);//ling zhi i love You//
        //4、根据指定分割符,分割字符串,末尾连续的分隔符会被忽视;如果开头有分隔符,第一个元素会返回’‘
        String[] split_s = s.split(" ");
        System.out.println(Arrays.toString(split_s));//[, ling, zhi, i, love, You]
        //5、将字符串的每一位转成ASCII码,并以数组的形式返回
        byte[] bytes_s=s.getBytes();
        System.out.println(Arrays.toString(bytes_s));//[32, 108, 105, 110, 103, 32, 122, 104, 105, 32, 105, 32, 108, 111, 118, 101, 32, 89, 111, 117, 32]
        //6、字符串转为小写字母组成
        String low_s=s.toLowerCase();
        System.out.println(low_s);// ling zhi i love you  //
        //7、字符串转为大写字母组成
        String up_s=s.toUpperCase();
        System.out.println(up_s);// LING ZHI I LOVE YOU  //
        //8、截取子串,包含左边界,不包含右边界
        String sub_s=s.substring(0,4);
        System.out.println(sub_s);// lin
        String sub_s2=s.substring(0,5);
        System.out.println(sub_s2);// ling
        //9、判断字符串是否相等
        boolean no_equal= " ling zhi ".equals(s);
        System.out.println(no_equal);//false
        boolean equal= " ling zhi i love You ".equals(s);
        System.out.println(equal);//true

    }
}
  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值