String类中的常用方法的使用

认识String类

常见构建String类的方式

方式1:

	String str1 = "hello world";

方式2:

	String str2 = new String("hello world");

方式3:

	char[] arr = {
    
    'a', 'b', 'c'};
    String str3 = new String(arr);

注意:
String是引用类型,str指向一个对象。
String类型一旦创建,就很难被修改。

String常见方法

length()

获取字符串的长度,这里需要注意的是和数组中长度的length的区别,这里的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]

注意
学会前面三种方式,我们就可以遍历字符串了,可以将字符串转换为字符数组,采用for或者for-each循环遍历字符数组。

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);
    }

结果:
在这里插入图片描述
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);
    }

结果:
在这里插入图片描述
lastIndexOf同样会找到第一次出现的位置,并返回第一次出现的下标索引。也可以指定位置,从指定位置开始向前查找

substring()

从指定的位置截取到结尾(或截取到指定位置)
注意:
索引从0开始,才去前闭区,后开区间的方式,例如substring(0,5),表示包含0号下标,不包含5号下标

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);
    }

在这里插入图片描述

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);
    }

在这里插入图片描述

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));
    }

在这里插入图片描述

static join()

将字符串拆分后,还会以什么拼接起来;
注意这是一个静态方法,直接通过类名.join()调用

    public static void main(String[] args) {
    
    
        String s = "hello world hello java!";
        String[] a = s.split(" ");//以空格拆分
        String b = String.join("/", a);

        System.out.println(Arrays.toString(a));
        System.out.println(b);
    }

在这里插入图片描述

toLowerCase()

字符串转小写,将字符串中大写的字母转换成小写

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

结果是:

hello world hello java!

toUpperCase()

字符串转大写,将字符串中小写的字母转换成大写

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

结果:HELLO WORLD HELLO 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);
    }

在这里插入图片描述

statsWith()

(从指定位置开始)判断是否以指定字符串开头

public static void main(String[] args) {
    
    
        String s = "Hello World Hello Java!";
        boolean a = s.startsWith("he");
        boolean b = s.startsWith("He");
        boolean c = s.startsWith("ll", 2);
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
    }

在这里插入图片描述

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);
    }

结果:
在这里插入图片描述

trim()

去掉字符串左右空格,换行等,保留中间的空格

public static void main(String[] args) {
    
    
        String s = "     |Hello World|     |Hello Java!|      \n";
        String a = s.trim();
        System.out.println(a);

    }

在这里插入图片描述

字符串的比较

1、==

==比较两个字符串的时候,比较的是他们是否指向同一个对象

public static void main(String[] args) {
    
    
        String a="hello";
        String b="hello";
        System.out.println(a==b);
        String c=new String("hello");
        System.out.println(a==c);
    }

结果:

true
false

因为相同内容会被java优化放在字符常量池中,所以第一个是true,因为他们都指向一个对象,第二个指向不同对象,所以是false

2、equals()和equalsIgnoreCase()

比较两个字符串的内容是否相等,前者区分大小写,后者不区分大小写,返回boolean值

    public static void main(String[] args) {
    
    
        String a = "hello";
        String b = "Hello";
        String c = "Hello";
        boolean st1 = a.equals(b);
        boolean st2 = a.equalsIgnoreCase(c);
        boolean st3 = b.equalsIgnoreCase(c);
        System.out.println(st1);
        System.out.println(st2);
        System.out.println(st3);
    }

结果:

false
true
true

3、compareTo()和compareToIgnoreCase()

比较两个字符串的内容是否不相等,前者区分大小写,后者不区分大小写,返回值等于0表示相等,大于0 表示大于,小于0 表示小于
例如:

    public static void main(String[] args) {
    
    
        String a = "hello";
        String b = "Hello";
        String c = "Hello";
        int st1 = a.compareTo(b);
        int st4 = b.compareTo(a);
        int st2 = b.compareTo(c);
        int st3 = a.compareToIgnoreCase(c);
        System.out.println(st1);
        System.out.println(st2);
        System.out.println(st3);
        System.out.println(st4);
    }

在这里插入图片描述

4、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);
    }

在这里插入图片描述

StringBuilder

因为String的类型一旦创建就很难修改,而当我们想要修改的时候,就是让引用指向新的对象,这样会造成很大的空间浪费
例如:

public static void main(String[] args) {
    
    
        String s = "H";     // 创建一个对象 "H"

        for (int i = 0; i < 10; i++) {
    
    
            s = s + i;      // 创建一个新的对象
        }

        System.out.println(s);
    }

结果是:H0123456789
我们不停地让s指向新的对象。这里新创建了9个对象

解决办法:

    public static void main(String[] args) {
    
    
        StringBuilder sb = new StringBuilder("H");     // 创建一个对象 "H"

        for (int i = 0; i < 10; i++) {
    
    
            sb.append(i);      // 创建一个新的对象
        }

        String s = sb.toString();

        System.out.println(s);
    }

结果是:H0123456789
通过 StringBuilder 创建一个对象,然后用append加入内容,最后通过toString转成String类型,这样就不会造成空间的浪费了。

猜你喜欢

转载自blog.csdn.net/weixin_52142731/article/details/113189636