String 类常用方法的使用

目的

之前系统学习的时候对String类的许多方法还不是很了解,使用起来也不熟悉,以至于好多算法题实现不了,现在重新复盘一下String的一些常用方法。

String类的常用方法

length() 获取字符串的长度

注意:String 类中 length 是方法 length() str.length(),数组中是属性 length arr.length

public static void main(String[] args) {
        String s = "hello world";
        int a = s.length();
        System.out.println(a);
    }

结果输出:11
注意:空格也算是一个字符长度

charAt() 获取指定位置的字符,从0开始

 public static void main(String[] args) {
        String s = "hello world";
        char a=s.charAt(1);
        System.out.println(a);
    }

结果输出:e

toCharArray() 将字符串变为字符数组返回

public static void main(String[] args) {
        String s = "hello world";
        char[]arr =s.toCharArray();
        System.out.println(Arrays.toString(arr));
    }

结果输出:[h,e,l,l,o, ,w,o,r,l,d]
toCharArray()方法得到的是一个数组对象,如果需要分成数组格式[a,b,c]这样,一般和Arrays.toString()使用 重写得到一个字符数组

indexOf() 从指定位置 从前往后 查找指定字符串的位置

查到了返回所在位置索引 没找到返回 -1

public static void main(String[] args) {
        String s = "hello world hello java!";
        int a= s.indexOf('h');
        int b=s.indexOf('z');
        int c=s.indexOf("hello");
        int d=s.indexOf("hello",3);
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
        System.out.println(d);
    }

结果输出:0 -1 0 12

第一个返回0 第二个找不到返回-1 第三个返回0 第四个从3号位置l开始找到 12

indexOf会找到第一次出现的位置,并返回第一次出现的下标索引。也可以指定位置,从指定位置开始向后查找

lastIndexOf() 返回指定字符在此字符串中最后一次出现处的索引 反向搜索

查到了返回其所在位置索引,没找到返回-1

 public static void main(String[] args) {
        String s = "hello world hello java!";
        int a= s.lastIndexOf('h');
        int b=s.lastIndexOf('z');
        int c=s.lastIndexOf("hello");
        int d=s.lastIndexOf("hello",5);//从指定的索引开始反向搜索
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
        System.out.println(d);
    }

结果输出:12 -1 12 0

返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索,如果此字符串中没有这样的字符,则返回 -1

substring() 从指定的位置截取到结尾 或截取到指定位置

public static void main(String[] args) {
        String s = "hello world hello java!";
        String a = s.substring(3);
        String b = s.substring(2, 10);
        System.out.println(a);
        System.out.println(b);
    }

结果输出:lo world hello java! llo worl
注意:索引从0开始,前闭后开 区间的方式

replace() 字符串的替换,将指定字符或者字符串替换,

如果要替换的字符,原字符串中没有,那么则不进行替换

public static void main(String[] args) {
        String s = "hello world hello java!";
        String a = s.replace("o", "a");
        System.out.println(a);
        String b = s.replace('0', ' ');
        System.out.println(b);
    }

输出结果:hella warld hella java! hello world hello java!
第二个没找到 不替换

split() 字符串的拆分成一个String[]数组

public static void main(String[] args) {
        String s = "hello world hello java!";
        String[] a = s.split(" ");//以空格拆分
        String[] b = s.split("o", s.length());//用o拆分,数组最长不超过字符串长度
        System.out.println(Arrays.toString(a));
        System.out.println(Arrays.toString(b));
    }

结果输出:

【hello,world,hello,java!】 【hell,w,rld,hell,java!】

contains() 当且仅当此字符串包含指定的char值序列时才返回true。

查找该序列,如果其中包含有所查找的内容返回true。

public static void main(String[] args) {
        String s = "Hello World Hello Java!";
        boolean a = s.contains("o");
        System.out.println(a);
    }

结果输出:true

concat() 将两个字符串拼接起来,相当于“+”

 public static void main(String[] args) {
        String s = "Hello World";
        String t="Hello Java!";
        String a = s.concat(t);
        System.out.println(a);
    }

结果输出:Hello WorldHello Java!

endsWith() 判断是否以指定字符串结尾

 public static void main(String[] args) {
        String s = "Hello World Hello Java!";
        boolean a = s.endsWith("java!");
        boolean b = s.endsWith("Java!");
        System.out.println(a);
        System.out.println(b);
    }

结果输出:false true

isEmpty() 判断是否为空字符串,但不是null,而是长度为0,即 length()==0;

public static void main(String[] args) {
        String s1=" ";
        String s2="";
        boolean a=s1.isEmpty();
        boolean b=s2.isEmpty();
        System.out.println(a);
        System.out.println(b);
    }

结果输出:false true
长度为0 则为true

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值