Java 不可修改的String类的原因与比较方法 + 可修改的StringBuilder类 /StringBuffer类常用方法


前言

String类是使用频率极高的一个引用类型,因此对String类的学习也显得尤为重要。


一、String类

  1. String是引用类型,其内部并没有存储字符串本身,而是存储了字符串地址。在这里插入图片描述
// s1和s2引用的是不同对象 s1和s3引用的是同一对象
String s1 = new String("hello");
String s2 = new String("world");
String s3 = s1;
System.out.println(s1.length()); // 获取字符串长度---输出5
System.out.println(s1.isEmpty()); // 如果字符串长度为0,返回true,否则返回false

在这里插入图片描述


  1. 在Java中""引起来的也是String类型对象。

二、常用方法

1.String类构造

String类提供的构造方式非常多,常用的有以下三种

// 使用常量串构造
String s1 = "hello world";


// 直接newString对象
String s2 = new String("hello world");


// 使用字符数组进行构造
char[] array = {'h','e','l','l','o','w','o','r','l','d'};
String s3 = new String(array);

2.String对象的比较

1. ==

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

	public static void main(String[] args) {
        int a = 10;
        int b = 10;
        int c = 20;
        //对于基本类型来说,==是比较两个变量中存储的值是否相同
        System.out.println(a == b);//true
        System.out.println(a == c);//false
        


        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(s1 == s3);//false
        System.out.println(s1 == s4);//true

    }

2. boolean equals(object anObject)方法

	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("world");

		// equals比较:String对象中的逐个字符
		// 虽然s1与s2引用的不是同一个对象,但是两个对象中放置的内容相同,因此输出true
		// s1与s3引用的不是同一个对象,而且两个对象中内容也不同,因此输出false
        System.out.println(s1.equals(s2));//true
        System.out.println(s1.equals(s3));//false
    }

3. 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
}

4. 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
}

2.字符串查找

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

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

注意:上述方法都是实例方法。


3.转化

  1. 数值和字符串转化
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("xiaoming", 18));
	System.out.println(s1);
	System.out.println(s2);
	System.out.println(s3);
	System.out.println(s4);
	System.out.println("=================================");
	// 字符串转数字
	// 注意:Integer、Double等是Java中的包装类型,这个后面会讲到
	int data1 = Integer.parseInt("1234");
	double data2 = Double.parseDouble("12.34");
	System.out.println(data1);
	System.out.println(data2);
}

  1. 大小写转换
public static void main(String[] args) {
	String s1 = "hello";
	String s2 = "HELLO";
	// 小写转大写
	System.out.println(s1.toUpperCase());
	// 大写转小写
	System.out.println(s2.toLowerCase());
}

  1. 字符串转数组
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. 格式化
public static void main(String[] args) {
	String s = String.format("%d-%d-%d", 2019, 9,14);
	System.out.println(s);
}

4.字符串替换

使用一个指定的新的字符串替换掉已有的字符串数据,可用的方法如下:

String replaceAll(String regex, String replacement)String replaceFirst(String regex, String replacement)
替换所有的指定内容替换首个内容
String str = "helloworld" ;
System.out.println(str.replaceAll("l", "_"));
System.out.println(str.replaceFirst("l", "_"));

注意事项: 由于字符串是不可变对象, 替换不修改当前字符串, 而是产生一个新的字符串


5.字符串拆分

可以将一个完整的字符串按照指定的分隔符划分为若干个子字符串。
可用方法如下:

String[] split(String regex)String[] split(String regex, int limit)
将字符串全部拆分将字符串以指定的格式,拆分为limit组
//字符串的全部拆分处理
String str = "hello world hello world" ;
String[] result = str.split(" ") ; // 按照空格拆分
for(String s: result) {
	System.out.println(s);
}

//字符串的部分拆分
String str = "hello world hello world" ;
String[] result = str.split(" ",2) ;
for(String s: result) {
	System.out.println(s);
}
//拆分ip地址
String str = "192.168.1.1" ;
String[] result = str.split("\\.") ;
for(String s: result) {
	System.out.println(s);
}

拆分是特别常用的操作. 一定要重点掌握. 另外有些特殊字符作为分割符可能无法正确切分, 需要加上转义


注意事项:

  1. 字符" | “,” * “,”+"都得加上转义字符,前面加上 “\\” .
  2. 而如果是 “\” ,那么就得写成 “\\\\” .
  3. 如果一个字符串中有多个分隔符,可以用"|"作为连字符.

6.字符串截取

从一个完整的字符串之中截取出部分内容。可用方法如下:

String substring(int beginIndex)String substring(int beginIndex, int endIndex)
从指定索引截取到结尾截取部分内容
String str = "helloworld" ;
System.out.println(str.substring(5));
System.out.println(str.substring(0, 5));


注意事项:

  1. 索引从0开始
  2. 注意前闭后开区间的写法, substring(0, 5) 表示包含 0 号下标的字符, 不包含 5 号下标

7.其他操作方法

String trim()String toUpperCase()String toLowerCase()
去掉字符串中的左右空格,保留中间空格字符串转大写字符串转小写
String str = " hello world " ;
System.out.println(str);
System.out.println(str.trim());
//trim 会去掉字符串开头和结尾的空白字符(空格, 换行, 制表符等)
String str = " hello%$$%@#$%world 你好 " ;
System.out.println(str.toUpperCase());
System.out.println(str.toLowerCase());
//这两个方法只转换字母。

8.字符串的不可变性

  1. String类在设计时就是不可改变的,String类实现描述中已经说明了
    String类中的字符实际保存在内部维护的value字符数组中
  2. 所有涉及到可能修改字符串内容的操作都是创建一个新对象,改变的是新对象
    public String replace(char oldChar, char newChar) {
        if (oldChar != newChar) {
            int len = value.length;
            int i = -1;

			//申请了一个新的字符数组
            char[] val = value; /* avoid getfield opcode */

            while (++i < len) {
                if (val[i] == oldChar) {
                    break;
                }
            }
            if (i < len) {
            	//申请了第二个新的字符数组
                char buf[] = new char[len];
                for (int j = 0; j < i; j++) {
                    buf[j] = val[j];
                }
                while (i < len) {
                    char c = val[i];
                    buf[i] = (c == oldChar) ? newChar : c;
                    i++;
                }
                //new了一个新的String类
                return new String(buf, true);

				//所以要修改字符串会额外创建三个临时对象
            }
        }
        return this;
    }

9.字符串的修改

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

public static void main(String[] args) {
	String s = "hello";
	s += " world";//'+='就相等于replace方法
	System.out.println(s); // 输出:hello world
}

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

结果显而易见,String类的修改效率极其低下,
而StringBuffer类和StringBuilder类的效率就很高,
因为字符串的修改最好借助StringBuffer类和StringBuilder类


三、StringBuilder和StringBuffer

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


public static void main(String[] args) {
		//StringBuilder类的创建
        String a = "hello";
        StringBuilder b = new StringBuilder(a);//直接传入已有的String类变量
        StringBuilder c = new StringBuilder("hello");
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
    }
StringBuilder append(String str)在尾部追加,相当于String的+=,可以追加:boolean、char、char[]、double、float、int、long、Object、String、StringBuilder的变量
char charAt(int index)获取index位置的字符
int length()获取字符串的长度
String toString()将所有字符按照String的方式返回

总结

以上就是今天要讲的内容,本文分享了String,StringBuilder类,StringBuffer类以及其中的一些方法。

路漫漫,不止修身也养性。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值