Java字符串类
String
-
特点:
- 一旦赋值,便不能更改其指向的字符对象;如果更改,则会指向一个新的字符对象,即字符串是一个常量
- 传参特点:尽管是传引用,但通过形参引用不会改变实参指向的字符串内容
-
比较:
-
==比较:,引用比较,比较两个对象是否引用同一个实例,比较的不是字符串对象的内容,而是对象的堆内存地址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 } }
- 常用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
字符串变量,其实例对象可以修改、扩充
- 常用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
字符串缓冲区,长度可以动态改变的数组
- 常用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 } }
三种字符串类的区别和转换
区别
| String | StringBuffer | StringBulider |
|---|---|---|
| 每次产生新的String对象,效率低,浪费大量内存空间 | 不产生新的对象,效率较低,容量可变 | 不产生新的对象,效率高,速度快,容量可变 |
| 不可变,字符串常量 | 可变 | 可变 |
| 线程安全 | 线程不安全 | |
| 多线程操作 | 单线程操作 |
转换
String转换成StringBuffer、StringBulider
String str = "abcd";
StringBuffer strbf = new StringBuffer(str);
StringBuilder strbl = new StringBuilder(str);
System.out.println(strbf); // abcd
System.out.println(strbl); // abcd
StringBuffer、StringBulider互相转换
// 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
StringBuffer、StringBulider转换成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,频繁创建新对象导致效率低下和空间资源的浪费
建议使用StringBuffer或StringBuilder
总结
Java对字符串的处理进行了很完整的封装,应具体了解每一个字符串类的特点与缺点,将他们应用于正确合适的场景
在整理博客的过程中参考了一些资料以及许多他人优秀的文章,就不一一列举,在此表示感谢。

被折叠的 条评论
为什么被折叠?



