• String类


    String 的重要性

    字符串(String)在程序设计中被广泛使用,C语言也可以通过字符数组或字符指针的方式来完成字符操作,但是这种做法将数据和操作分离了,不符合面相对象的思想,java专门提供了String及其相关的一系列类来管理字符串.

    String类的常用方法

    构造方法

    使用字符串常量构造

    String str1 = "hello world";
    
    • 1

    new String对象

    在这里插入图片描述

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

    使用字符数组进行构造

    在这里插入图片描述

    char[] str = {'h','e','l','l','o','w','o','r','l','d'};
    String str3 = new String(str);
    
    • 1
    • 2

    String 对象比较

    ==

    • 对于内置类型==比较的是变量中的值
    • 对于引用类型==比较的是引用中的地址

    而String类型是字符串应用类型,所以使用 == 只能比较两个字符串是否引用了同一个对象.

    练习:

    public class Test {
        public static void main(String[] args) {
            int a = 10;
            int b = 20;
            int c = 10;
            // 对于基本类型变量,==比较两个变量中存储的值是否相同
            System.out.println(a == b); // false
            System.out.println(a == c); // true
            // 对于引用类型变量,==比较两个引用变量引用的是否为同一个对象
            String s1 = new String("hello");
            String s2 = new String("hello");
            String s3 = new String("world");
            String s4 = s1;
            System.out.println(s1 == s2); // false
            System.out.println(s2 == s3); // false
            System.out.println(s1 == s4); // true
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    总结:

    • 字符串常量会存放到字符串常量池,多次使用指向同一个对象
    • 使用new 关键字创建的对象,会有新的地址

    equals

    String类重写了父类Object中equals方法,Object中equals默认按照==比较,String重写equals方法后,按照字典序进行比较,即字符的大小顺序.

    public class Test {
        public static void main(String[] args) {
            String s1 = new String("hello");
            String s2 = new String("hello");
            String s3 = new String("Hello");
            // s1、s2、s3引用的是三个不同对象,因此==比较结果全部为false
            System.out.println(s1 == s2); // false
            System.out.println(s1 == s3); // false
            // equals比较:String对象中的逐个字符
            // 虽然s1与s2引用的不是同一个对象,但是两个对象中放置的内容相同,因此输出true
            // s1与s3引用的不是同一个对象,而且两个对象中内容也不同,因此输出false
            System.out.println(s1.equals(s2)); // true
            System.out.println(s1.equals(s3)); // false
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    compareTo

    与equals不同的是,equals返回的是boolean类型,而compareTo返回的是int类型。

    • equals 只能比较是否相等
    • compareTo可以比较大小

    具体比较方式:

    1. 先按照字典次序大小比较,如果出现不等的字符,直接返回这两个字符的大小差值
    2. 如果前k个字符相等(k为两个字符长度最小值),返回值两个字符串长度差值
    public class Test {
        public static void main(String[] args) {
            String s1 = new String("abc");
            String s2 = new String("ac");
            String s4 = new String("abcdef");
            System.out.println(s1.compareTo(s2)); // 不同输出字符差值-1
            System.out.println(s1.compareTo(s4)); // 前k个字符完全相同,输出长度差值 -3
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    compareToIgnoreCase

    与compareTo方式相同,但是忽略大小写比较,这里不作介绍

    字符串查找

    方法功能
    char charAt(int index)返回index位置上字符,如果index为负数或者越界,抛出IndexOutOfBoundsException异常
    int indexOf(int ch)返回ch第一次出现的位置,没有返回-1
    int indexOf(int ch, intfromIndex)从fromIndex位置开始找ch第一次出现的位置,没有返回-1
    int indexOf(String str)返回str第一次出现的位置,没有返回-1
    int indexOf(String str, int fromIndex)从fromIndex位置开始找str第一次出现的位置,没有返回-1
    int lastIndexOf(int ch)从后往前找,返回ch第一次出现的位置,没有返回-1
    int lastIndexOf(int ch, int fromIndex)从fromIndex位置开始找,从后往前找ch第一次出现的位置,没有返回-1
    int lastIndexOf(String str)从后往前找,返回str第一次出现的位置,没有返回-1
    int lastIndexOf(String str, int fromIndex)从fromIndex位置开始找,从后往前找str第一次出现的位置,没有返回-1
    public class Test {
        public static void main(String[] args) {
            String s = "aaabbbcccaaabbbccc";
            System.out.println(s.charAt(3)); // 'b'
            System.out.println(s.indexOf('c')); // 6
            System.out.println(s.indexOf('c', 10)); // 15
            System.out.println(s.indexOf("bbb")); // 3
            System.out.println(s.indexOf("bbb", 10)); // 12
            System.out.println(s.lastIndexOf('c')); // 17
            System.out.println(s.lastIndexOf('c', 10)); // 8
            System.out.println(s.lastIndexOf("bbb")); // 12
            System.out.println(s.lastIndexOf("bbb", 10));
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    转化

    数值和字符串转化

    public class Test {
        public static void main(String[] args) {
            // 数字转字符串
            String s1 = String.valueOf(1234);
            String s2 = String.valueOf(12.34);
            String s3 = String.valueOf(true);
            System.out.println(s1);
            System.out.println(s2);
            System.out.println(s3);
            System.out.println("=================================");
            // 字符串转数字
            int data1 = Integer.parseInt("1234");
            double data2 = Double.parseDouble("12.34");
            System.out.println(data1);
            System.out.println(data2);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    大小写转换

    public class Test {
        public static void main(String[] args) {
            String s1 = "hello";
            String s2 = "HELLO";
            // 小写转大写
            System.out.println(s1.toUpperCase());
            // 大写转小写
            System.out.println(s2.toLowerCase());
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    字符串转数组

    public class Test {
        public static void main(String[] args) {
            String s = "hello";
            // 字符串转数组
            char[] ch = s.toCharArray();
            for (int i = 0; i < ch.length; i++) {
                System.out.print(ch[i]);
            }
            System.out.println();
            // 数组转字符串
            String s2 = new String(ch);
            System.out.println(s2);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    格式化

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

    字符串替换

    在这里插入图片描述
    后面的方法就不作代码演示了,感兴趣的可以自己体验

    字符串拆分

    方法功能
    String[] split(String regex)将字符串全部拆分
    String[] split(String regex, int limit)将字符串以指定的格式,拆分为limit组

    字符串截取

    方法功能
    String substring(int beginIndex)从指定索引截取到结尾
    String substring(int beginIndex, int endIndex)截取部分内容
    在这里插入图片描述

    字符串的不可变性

    String是一种不可变对象. 字符串中的内容是不可改变.。字符串不可被修改,是因为:

    1. String类在设计时就是不可改变的,String类实现描述中已经说明了
      在这里插入图片描述
    2. 所有涉及到可能修改字符串内容的操作都是创建一个新对象,改变的是新对象

    StringBuilder 和 StringBuffer

    由于String的不可更改特性,为了方便字符串的修改,java中又提供StringBuilder和StringBuffer类。

    方法

    在这里插入图片描述

    String、StringBuffer、StringBuilder的区别

    • String的内容不可修改,StringBuffer与StringBuilder的内容可以修改.
    • StringBuffer与StringBuilder大部分功能是相似的
    • StringBuffer采用同步处理,属于线程安全操作;而StringBuilder未采用同步处理,属于线程不安全操作
  • 相关阅读:
    c期末复习
    QEMU模拟ATF启动
    76-SpringSecurity介绍
    Leetcode 1492.n的第k个因子
    2-3.spring源码--BeanFactoryPostProcessor
    信息学奥赛一本通 1852:【08NOIP提高组】火柴棒等式 | 洛谷 P1149 [NOIP2008 提高组] 火柴棒等式
    日渐流行的零代码,究竟适用哪些场景?
    如何在Docker部署Drupal并结合内网穿透实现远程访问
    github下载源码失败(mac)
    【小程序】快来开发你的第一个微信小游戏(详细流程)
  • 原文地址:https://blog.csdn.net/qq_75209065/article/details/134351458