【Java】String类

字符串构造

String类提供了很多的构造方式 常用的就三种

public static void main(String[] args) {
        String string1 = "abc";
        //常量字符串构造
        String string2 = new String("abc");
        //new对象构造
        char[] arr = new char[]{'a','b','c'};
        String string3 = new String(arr);
        //利用字符数组构造
        System.out.println(string1);
        System.out.println(string2);
        System.out.println(string3);
    }

在这里插入图片描述

字符串对象的比较

==比较是否引用同一个对象

注意:对于内置类型,比较的是变量中的值;对于引用类型比较的是引用中的地址。

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
    }

boolean equals(Object anObject) 方法:按照字典序比较

字典序:字符大小的顺序
String类重写了父类Object中equals方法,Object中equals默认按照==比较,String重写equals方法后,按照如下规则进行比较,
比如: s1.equals(s2)

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

int compareTo(String s) 方法: 按照字典序进行比较

与equals不同的是,equals返回的是boolean类型,而compareTo返回的是int类型。具体比较方式:

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

int compareToIgnoreCase(String str) 方法:与compareTo方式相同,但是忽略大小写比较

public static void main(String[] args) {
        String s1 = new String("abc");
        String s2 = new String("ac");
        String s3 = new String("ABc");
        String s4 = new String("abcdef");
        System.out.println(s1.compareToIgnoreCase(s2)); // 不同输出字符差值-1
        System.out.println(s1.compareToIgnoreCase(s3)); // 相同输出 0
        System.out.println(s1.compareToIgnoreCase(s4)); // 前k个字符完全相同,输出长度差值 -3
    }

字符串查找

字符串查找也是字符串中非常常见的操作,String类提供的常用查找的方法:

char charAt(int index)

返回index位置上字符,如果index为负数或者越界,抛出
IndexOutOfBoundsException异常

public static void main(String[] args) {
        String str = new String("abcdefg");
        char ch1 = str.charAt(1);
        char ch2 = str.charAt(3);
        System.out.println(ch1);
        System.out.println(ch2);
    }

在这里插入图片描述

int indexOf(int ch)

返回ch第一次出现的位置,没有返回-1

public static void main(String[] args) {
        String str = new String("abcdefg");
        int index1 = str.indexOf('c');
        int index2 = str.indexOf('z');
        System.out.println(index1);
        System.out.println(index2);
    }

在这里插入图片描述

int indexOf(int ch, int fromIndex)

从fromIndex位置开始找ch第一次出现的位置,没有返回-1

public static void main(String[] args) {
        String str = new String("abcdefg");
        int index1 = str.indexOf('c',1);
        int index2 = str.indexOf('z',1);
        System.out.println(index1);
        System.out.println(index2);
    }

在这里插入图片描述

int indexOf(String str)

返回str第一次出现的位置,没有返回-1

public static void main(String[] args) {
        String str = new String("abcdefghijklmn");
        int index1 = str.indexOf("cde");
        int index2 = str.indexOf("zxc");
        System.out.println(index1);
        System.out.println(index2);
    }

在这里插入图片描述

int indexOf(String str, int fromIndex)

和int indexOf(int ch) 一致

int lastIndexOf(int ch)

从后往前找,返回ch第一次出现的位置,没有返回-1
lastIndexOf其他用法和indexOf相同 都是重载的方法

转化

数值和字符串转化

数字转字符串

class Student {
    String name;
    int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

public class test1 {
    public static void main(String[] args) {
        String s1 = String.valueOf(1234);
        String s2 = String.valueOf(12.34);
        String s3 = String.valueOf(true);
        String s4 = String.valueOf(new Student("zhangsan", 18));
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);
        System.out.println(s4);
        //可以将很多类型转化为字符串
        //甚至是一个自定义的类 只要重写了toString方法就可以做到
    }
}

在这里插入图片描述

字符串转数字

public static void main(String[] args) {
        String s1 = "123456";
        String s2 = "123.456";

        int num1 = Integer.valueOf(s1);
        double num2 = Double.valueOf(s2);
		// 注意:Integer、Double等是Java中的包装类型
        System.out.println(num1);
        System.out.println(num2);
    }

在这里插入图片描述

大小写转化

public static void main(String[] args) {
        String s1 = "abcDEF";
        String s2 = "ABCdef";
        String s3 = s1.toLowerCase();
        String s4 = s2.toUpperCase();
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);
        System.out.println(s4);
		//不是修改原字符串 
    }

在这里插入图片描述

字符串转数组

public static void main(String[] args) {
        String s1 = "abcdefg";
        char[] array = s1.toCharArray();
        for (int i = 0; i < array.length ; i++) {
            System.out.print(array[i] + " ");
        }
    }

在这里插入图片描述

格式化

public static void main(String[] args) {
        String s = String.format("%d-%d-%d", 2019, 9,14);
        //有种c语言的感觉
        System.out.println(s);
    }

在这里插入图片描述

字符串替换

public static void main(String[] args) {
        String s1 = "aaabbbcccdddaaabbbcccddd";
        String s2 = s1.replaceFirst("aaa","zzz");
        String s3 = s1.replaceAll("aaa","vvv");
        System.out.println(s1);
        //原字符串没有改变
        System.out.println(s2);
        //replaceFirst只修改第一个
        System.out.println(s3);
        //replaceAll修改所有出现的
    }

在这里插入图片描述

字符串拆分

public static void main(String[] args) {
        String s1 = "I am a good boy!";
        String[] arr1 = s1.split(" ");
        String[] arr2 = s1.split(" ",2);
        for (int i = 0; i < arr1.length; i++) {
            System.out.println(arr1[i]);
        }
        System.out.println();
        for (int i = 0; i < arr2.length; i++) {
            System.out.println(arr2[i]);
        }
    }

可以将一个完整的字符串按照指定的分隔符划分为若干个子字符串。
在这里插入图片描述
在这里插入图片描述

字符串截取

 public static void main(String[] args) {
        String s1 = "hello world!!!";
        System.out.println(s1.substring(5));
        System.out.println(s1.substring(3,9));
    }

在这里插入图片描述

其他操作方法

在这里插入图片描述

StringBuilder和StringBuffer

字符串的不可变性

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

  1. String类在设计时就是不可改变的,String类实现描述中已经说明了

在这里插入图片描述
String类中的字符实际保存在内部维护的value字符数组中,该图还可以看出:

  1. String类被final修饰,表明该类不能被继承
  2. value被修饰被final修饰,表明value自身的值不能改变,即不能引用其它字符数组,但是其引用空间中的内容可以修改。

所有涉及到可能修改字符串内容的操作都是创建一个新对象,改变的是新对象

final修饰类表明该类不想被继承,final修饰引用类型表明该引用变量不能引用其他对象,但是其引用对象中的内容是可以修改的。

字符串修改

注意:尽量避免直接对String类型对象进行修改,因为String类是不能修改的,所有的修改都会创建新对象,效率非常低下。

public static void main(String[] args) {
	String s = "hello";
	s += " world";
	System.out.println(s); // 输出:hello world
}

但是这种方式不推荐使用,因为其效率非常低,中间创建了好多临时对象。

StringBuilder的介绍

由于String的不可更改特性,为了方便字符串的修改,Java中又提供StringBuilder和StringBuffer类。这两个类大部分功能是相同的,这里介绍 StringBuilder常用的一些方法

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

沉着的码农

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值