Java字符串类

Java字符串类

String

  1. 特点:

    • 一旦赋值,便不能更改其指向的字符对象;如果更改,则会指向一个新的字符对象,即字符串是一个常量
    • 传参特点:尽管是传引用,但通过形参引用不会改变实参指向的字符串内容
  2. 比较:

  • ==比较:,引用比较,比较两个对象是否引用同一个实例,比较的不是字符串对象的内容,而是对象的堆内存地址

    public class Test {
    	public static void main(String[] args) {
    		String a = "hello";
    		String b = new String("hello");
    		String c = b;
    		System.out.println(a == b); // false
    		System.out.println(a == c); // false
    		System.out.println(b == c); // true
    	}
    }
    
    • equals比较:值比较,比较两个字符串内容是否相同,严格区分大小写
    public class Test {
    	public static void main(String[] args) {
    		String a = "hello";
    		String b = new String("hello");
    		String c = b;
    		System.out.println(a.equals(b)); // false
    		System.out.println(a.equals(c)); // false
    		System.out.println(b.equals(b)); // true
    	}
    }
    
  1. 常用API
  • concat(),字符串拼接

    public class Test {
        public static void main(String[] args) {
            String a="abcdefghijk";
            String b = "123456789";
            System.out.println(a.concat(b)); // abcdefghijk123456789
        }
    }
    
    • charAt(),返回指定位置的字符
    public class Test {
        public static void main(String[] args) {
            String a = "abcdefghijk";
            String b = "123456789";
            System.out.println(a.charAt(3)); // d
        }
    }
    
    • indexOf() / lastIndexOf(),返回某个字符第一次/最后一次出现的下标,没有就返回-1
    public class Test {
        public static void main(String[] args) {
            String a = "abcdefghijkabcd";
            System.out.println(a.indexOf('a')); // 0
            System.out.println(a.lastIndexOf('b')); // 12
        }
    }
    
    • toUpperCase() / toLowerCase(),字符串转换成大写/小写
    public class Test {
        public static void main(String[] args) {
            String a = "abcdEfghIjkABCD";
            System.out.println(a.toUpperCase()); // ABCDEFGHIJKABCD
            System.out.println(a.toLowerCase()); // abcdefghijkabcd
        }
    }
    
    • split(),切割字符串,返回字符串数组
    public class Test {
        public static void main(String[] args) {
            String a = "abcd-E-fgh-I-jk-ABCD";
            String[] b = a.split("-");
            for (String x: b) {
                System.out.print(x + " "); // abcd E fgh I jk ABCD 
            }
        }
    }
    
    • replace(),替换字符串的指定字符
    public class Test {
        public static void main(String[] args) {
            String a = "abcdefghijkabcd";
            System.out.println(a.replace('a','A')); // AbcdefghijkAbcd
        }
    }
    
    • substring(),截取字符串
    public class Test {
        public static void main(String[] args) {
            String a = "abcdefghijkabcd";
            System.out.println(a.substring(3,6)); // def
        }
    }
    

StringBuffer

字符串变量,其实例对象可以修改、扩充

  1. 常用API
  • append(),添加到字符串末尾

    public class Test {
        public static void main(String[] args) {
            StringBuffer a = new StringBuffer("abcdefghijkabcd");
            System.out.println(a.append('e')); // abcdefghijkabcde
        } 
    }
    
    • insert(),插入到字符串任意位置
    public class Test {
        public static void main(String[] args) {
            StringBuffer a = new StringBuffer("abcdefghijkabcd");
            StringBuffer b = new StringBuffer("123");
            System.out.println(a.insert(2,b)); // ab123cdefghijkabcd
        }
    }
    
    • deleteCharAt(),删除指定位置的字符
    public class Test {
        public static void main(String[] args) {
            StringBuffer a = new StringBuffer("abcdefghijkabcd");
            StringBuffer b = new StringBuffer("123");
            System.out.println(a.insert(2,b).deleteCharAt(2)); // ab23cdefghijkabcd
        }
    }
    
    • reserve(),字符串反转
    public class Test {
        public static void main(String[] args) {
            StringBuffer a = new StringBuffer("ab123456789cdefghijk");
            System.out.println(a.reverse()); // kjihgfedc987654321ba
        }
    }
    

StringBuilder

字符串缓冲区,长度可以动态改变的数组

  1. 常用API
  • append(),添加到字符串末尾

    public class Test {
        public static void main(String[] args) {
            StringBuilder a = new StringBuilder("abcdefghijk");
            System.out.println(a.append("abcd")); // abcdefghijkabcd
        }
    }
    
    • insert(),插入到字符串任意位置
    public class Test {
        public static void main(String[] args) {
            StringBuilder a = new StringBuilder("abcdefghijk");
            System.out.println(a.insert(2,"123")); // ab123cdefghijk
        }
    }
    
    • delete(),移除指定位置指定长度的字符
    public class Test {
        public static void main(String[] args) {
            StringBuilder a = new StringBuilder("ab12345cdefghijk");
            System.out.println(a.delete(2,7)); // abcdefghijk
        }
    }
    
    • repalce(),替换指定位置的字符
    public class Test {
        public static void main(String[] args) {
            StringBuilder a = new StringBuilder("ab12345cdefghijk");
            System.out.println(a.replace(2,7,"98765")); // ab98765cdefghijk
        }
    }
    

三种字符串类的区别和转换

区别

StringStringBufferStringBulider
每次产生新的String对象,效率低,浪费大量内存空间不产生新的对象,效率较低,容量可变不产生新的对象,效率高,速度快,容量可变
不可变,字符串常量可变可变
线程安全线程不安全
多线程操作单线程操作

转换

  1. String转换成 StringBufferStringBulider
String str = "abcd";
StringBuffer strbf = new StringBuffer(str);
StringBuilder strbl = new StringBuilder(str);

System.out.println(strbf); // abcd
System.out.println(strbl); // abcd
  1. StringBufferStringBulider互相转换
// StringBuffer -> StringBulider
StringBuffer strbf = new StringBuffer("abcd");
StringBuilder strbl = new StringBuilder(strbf);
System.out.println(strbl); // abcd

// StringBulider -> StringBuffer
StringBuilder strbl = new StringBuilder("1234");
StringBuffer strbf = new StringBuffer(strbl);
System.out.println(strbf); // 1234
  1. StringBufferStringBulider转换成String
StringBuffer strbf = new StringBuffer("abcd");
StringBuilder strbl = new StringBuilder("1234");

String str1 = strbf.toString();
String str2 = strbl.toString();

System.out.println(str1); // abcd
System.out.println(str2); // 1234

字符串拼接

不建议在循环体内使用+拼接

  • + 反编译后的代码,在for循环中,每次都是new了一个StringBuilder,然后再把String转成StringBuilder,再进行append,频繁创建新对象导致效率低下和空间资源的浪费

建议使用StringBufferStringBuilder

总结

​ Java对字符串的处理进行了很完整的封装,应具体了解每一个字符串类的特点与缺点,将他们应用于正确合适的场景

​ 在整理博客的过程中参考了一些资料以及许多他人优秀的文章,就不一一列举,在此表示感谢。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值