• Java SE String类(一):常用方法(上)


    1. 常用方法

    1.1 字符串构造

    String类的常用构造方法只有以下三种

    public class Main {
        public static void main(String[] args) {
            String s1 = "hello";//使用常量串进行构造
            String s2 = new String("hello");//创建String对象
            char[] array = {'h','e','l','l','o'};//使用字符数组
            String s3 = new String(array);
            System.out.println(s1);
            System.out.println(s2);
            System.out.println(s3);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    如要使用其他String方法,大家可以查看源码或者是jdk帮助手册
    【注意】

    1. String类是引用类型的对象内部其实并不存在字符串本身,我们可以查看String类的部分源码:
    public final class String
        implements java.io.Serializable, Comparable<String>, CharSequence,
                   Constable, ConstantDesc {
         private final byte[] value;//用于存字符串的数组,字符存储在这个数组中形成了字符串
         private int hash; // 默认为0
         public String(String original) {//对以上两个成员变量进行构造
            this.value = original.value;
            this.hash = original.hash;
            }
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    下面我们用一段代码来说明String是引用类型

    public class Main {
        public static void main(String[] args) {
            String s1 = "hello";
            String s2 = new String("world");
            char[] array = {'h','i'};//创建了3个不同的引用对象
            String s3 = new String(array);
            System.out.println(s1);
            System.out.println(s2);
            System.out.println(s3);
            String s4 = s3;//s4和s3指向了同一个引用
            System.out.println(s4);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    字符串的引用方式其实和我们前面提到的数组很类似,因为底层就是数组实现的,我们下面通过画图说明
    在这里插入图片描述
    2. 在Java中,直接用双引号引起来的对象也是String类

    System.out.println("hello");
    
    • 1

    1.2 字符串的常规方法

    1. 计算长度
    public class Main {
        public static void main(String[] args) {
            String s1 = "hello";
            String s2 = new String("world");
            char[] array = {'h','i'};//创建了3个不同的引用对象
            String s3 = new String(array);
            System.out.println(s1);
            System.out.println(s2);
            System.out.println(s3);
            String s4 = s3;//s4和s3指向了同一个引用
            System.out.println(s4);
            System.out.println(s3.length());
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    在这里插入图片描述
    我们在这里需要注意的一点是,Java中的字符串不想c语言一样有以‘\0’这样的转义字符结尾的说法有几个字母,字符串就是多长,在上面的运行结果我们可以看到,s3的长度为2

    1. 判空
    public class Main {
        public static void main(String[] args) {
            String s1 = "hello";
            String s2 = new String("");//s2为空字符
            char[] array = {'h','i'};//创建了3个不同的引用对象
            String s3 = new String(array);
            System.out.println(s1);
            System.out.println(s2);
            System.out.println(s3);
            String s4 = s3;//s4和s3指向了同一个引用
            System.out.println(s4);
            System.out.println(s3.length());
            System.out.println(s2.isEmpty());
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    在这里插入图片描述
    从上述结果我们可以看出,isEmpty方法如果字符串为空,返回ture,如果不为空,返回false

    1.3 String对象的比较

    字符串的比较也是常见的操作之一,比如:字符串的排序,Java中总共提供了4种方式

    1. == 比较是否引用同一个对象
      注意:对于内置类型,比较的是变量中的值,对于引用类型比较的是引用中的地址
    public class Main {
        public static void main(String[] args) {
            int a = 1;
            int b = 1;
            int c = 2;
            String s1 = "hello";
            String s2 = "hi";
            String s3 = "hello";
            String s4 = s1;
            System.out.println(a == b);//两个值相等
            System.out.println(a == c);//两个值不相等
            System.out.println(s1 == s2);//两个字符串不一样
            System.out.println(s3 == s1);//虽然两个字符串相同,但是指向的引用不同
            System.out.println(s1 == s4);//指向同一个引用
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    在这里插入图片描述

    需要注意的是,由于String是引用类型,虽然s1和s3相同,但是它们的引用不同,所以返回false
    那么我们如果想要比较两个字符串是否相同,我们有办法吗,当然有,我们下面来介绍它

    1. boolean equals(Object anObject) 方法:按照字典序比较
      字典序:按照字符的大小顺序
      String重写了父类Object方法,Objec中默认使用“==”进行比较,String重写后按照如下规则比较:
    public boolean equals(Object anObject) {
    	// 1. 先检测this 和 anObject 是否为同一个对象比较,如果是返回true
    	if (this == anObject) {
    		return true;
    	}
    	// 2. 检测anObject是否为String类型的对象,如果是继续比较,否则返回false
    	if (anObject instanceof String) {
    	// 将anObject向下转型为String类型对象
    		String anotherString = (String)anObject;
    		int n = value.length;
    	// 3. this和anObject两个字符串的长度是否相同,是继续比较,否则返回false
    	if (n == anotherString.value.length) {
    		char v1[] = value;
    		char v2[] = anotherString.value;
    		int i = 0;
    		// 4. 按照字典序,从前往后逐个字符进行比较
    		while (n-- != 0) {
    			if (v1[i] != v2[i])
    			return false;
    			i++;
    		}
    		return true;
    		}
    	}
    	return false;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    public class Main {
        public static void main(String[] args) {
            int a = 1;
            int b = 1;
            int c = 2;
            String s1 = "hello";
            String s2 = "hi";
            String s3 = "hello";
            String s4 = s1;
            System.out.println(a == b);//两个值相等
            System.out.println(a == c);//两个值不相等
            System.out.println(s1 == s2);//两个字符串不一样
            System.out.println(s3 == s1);//虽然两个字符串相同,但是指向的引用不同
            System.out.println(s1 == s4);//指向同一个引用
            System.out.println(s1.equals(s3));//两个字符串相同
            System.out.println(s2.equals(s1));//两个字符串不同
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    在这里插入图片描述
    上述代码我们可以看到,虽然s1和s3指向的是不同的引用,但是只要字符串相同,就返回true
    3. int compareTo(String anotherString) 方法:按照字典序比较
    与equals不同的是,equals返回的是boolean类型,而compareTo返回的是int类型。具体比较方式:

    1. 先按照字典次序大小比较,如果出现不等的字符直接返回这两个字符的大小差值
    2. 如果前k个字符相等(k为两个字符长度最小值),返回值两个字符串长度差值
    public class Main {
        public static void main(String[] args) {
            String s1 = "hello";
            String s2 = "hi";
            String s3 = "hello";
            String s4 = "helloaaa";
            System.out.println(s1 == s2);//两个字符串不一样
            System.out.println(s3 == s1);//虽然两个字符串相同,但是指向的引用不同
            System.out.println(s1 == s4);//指向同一个引用
            System.out.println(s1.equals(s3));//两个字符串相同
            System.out.println(s2.equals(s1));//两个字符串不同
            System.out.println(s1.compareTo(s2));//返回字典序差值
            System.out.println(s1.compareTo(s3));//相同返回0
            System.out.println(s1.compareTo(s4));//前几个字符相同,返回字符串长度只差
    
        }
     }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    在这里插入图片描述
    4. int compareToIgnoreCase(String str) 方法:与compareTo不同的是忽略大小写进行比较

    public class Main {
        public static void main(String[] args) {
            String s1 = "Hello";
            String s2 = "hi";
            String s3 = "hello";
            String s4 = "helloaaa";
            System.out.println(s1 == s2);//两个字符串不一样
            System.out.println(s3 == s1);//虽然两个字符串相同,但是指向的引用不同
            System.out.println(s1 == s4);//指向同一个引用
            System.out.println(s1.equals(s3));//两个字符串相同
            System.out.println(s2.equals(s1));//两个字符串不同
            System.out.println(s1.compareTo(s2));//返回字典序差值
            System.out.println(s1.compareTo(s3));//相同返回0
            System.out.println(s1.compareTo(s4));//前几个字符相同,返回字符串长度只差
            System.out.println(s1.compareToIgnoreCase(s3));//忽略大小写进行比较
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    在这里插入图片描述

    1.3 字符串查找

    字符串查找也是字符串中非常常见的操作,String类提供了如下查找方法:
    在这里插入图片描述

    public class Main {
        public static void main(String[] args) {
            String s = "aaabbbcccddd";
            System.out.println(s.charAt(2));
            System.out.println(s.indexOf('a'));
            System.out.println(s.indexOf('a',2));
            System.out.println(s.indexOf("bb"));
            System.out.println(s.indexOf("bb",4));
            System.out.println(s.lastIndexOf('d'));
            System.out.println(s.lastIndexOf('d',10));
            System.out.println(s.lastIndexOf("dd"));
            System.out.println(s.lastIndexOf("dd",10));
        }
     }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    在这里插入图片描述

    1.4 转化

    1. 数值和字符串转化
    public class Main {
        public static void main(String[] args) {
            String s1 = String.valueOf(12);
            String s2 = String.valueOf(12.12);
            String s3 = String.valueOf(true);
            System.out.println(s1);
            System.out.println(s2);
            System.out.println(s3);
            int a = Integer.parseInt(s1);
            double b = Double.parseDouble(s2);
            System.out.println(s1);
            System.out.println(s2);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    在这里插入图片描述
    2. 大小写转换

    public class Main {
        public static void main(String[] args) {
            String s1 = "me";
            String s2 = "YOU";
            System.out.println(s1.toUpperCase());
            System.out.println(s2.toLowerCase());
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    在这里插入图片描述
    注意:这里在转化的是一个新的字符串,不是在原来的字符串上修改,因为字符串具有不可变性
    3. 字符串转数组和字符串转数组

    public class Main {
        public static void main(String[] args) {
            String s1 = "abcdefg";
            char[] array = s1.toCharArray();
            System.out.println(Arrays.toString(array));
            String s2 = new String(array);
            System.out.println(s2);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    在这里插入图片描述
    4. 格式化字符串

    public class Main {
        public static void main(String[] args) {
            String s = String.format("%d-%d-%d",2024,3,11);
            System.out.println(s);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述

  • 相关阅读:
    Hadoop--Hadoop基准测试(读写)
    Flex & Bison 开始
    【BLE】蓝牙Profile
    HTML的学习-通过创建相册WEB学习HTML-第一部分
    vue-cli3.0初始化项目
    MacOS - Cpolar 在 Mac 上如何使用?
    springmvc复习(第二天)(黑马版)
    2023年最适合0基础上手的—学生管理系统制作,另赠福利:GUI学生管理系统源码
    机械硬盘,Win10系统,磁盘100%
    【自动驾驶解决方案】C++取整与保留小数位
  • 原文地址:https://blog.csdn.net/2301_80050796/article/details/136633365