对字符串的花式处理一直是现代应用系统的主要操作之一,也是对Java基础知识考察的重要方面。事实上,Java字符串类的底层是通过数组来实现的。具体来说,String类是固定长度的数组,StringBuffer和StringBuilder则是可变长度的,其底层是通过Arrays.copyOf的方法,复制了另外的一个数组,实现了一个内部扩容机制,从而实现一种“伪可变”。
Java字符串要点
1. String类是不可变类,一旦创建,包含在String对象中的字符数组是不可变的,直至该对象被回收。但是正如数组一样,可以更改对象的引用,指向另一个String对象。
2. StringBuffer在使用上呈现出一个可变的字符数组的对象,因此有增删查改的方法。该类通过synchronized同步方法实现线程安全。
3. StringBuilder的构造方法和API与StringBuffer类似,不过是线程不安全的,因此性能较高。
String类构造方法和方法说明
1 package org.leo.demo.arrays;
2
3 import java.io.UnsupportedEncodingException;
4 //因为String类的不可变性,因此所有的连接、截取操作,都不改变原字符串的值。
5 public class TestString {
6
7 public static void main(String[] args) throws UnsupportedEncodingException {
8 //字符串初始化
9 String s1 = "Hello World!";
10 System.out.println(s1);
11 //通过byte[]和码表来构造字符串
12 byte[] b = {21, 97, 12, 100};
13 String s3 = new String(b, "utf-8");//有UnsupportedEncodingException异常抛出
14 System.out.println(s3);
15
16 //返回字符串长度
17 String s2 = new String("aeiou");
18 System.out.println(s2.replace('a', '$'));
19 System.out.println("s2.length()" + s2.length());
20 //通过索引查找字符
21 System.out.println("s2.charAt(2):" + s2.charAt(2));
22 //查找索引
23 System.out.println("s2.indexOf('o')" + s2.indexOf('o'));
24 System.out.println("s2.indexOf(\"io\")" + s2.indexOf("io"));
25 //查找前缀/后缀
26 System.out.println("s2.endsWith(\"ih\")"+s2.endsWith("ih"));
27 System.out.println("s2.startsWith(\"ae\")" + s2.startsWith("ae"));
28 //字符串的比较
29 String s21 = new String("aeiouwww");
30 String s22 = new String("aewou");
31 System.out.println("s2.compareTo(s21):"+s2.compareTo(s21));
32 System.out.println("s2.compareTo(s22):"+s2.compareTo(s22));
33 StringBuffer sb1 = new StringBuffer("aeiousss");
34 System.out.println("s2.contentEquals(sb1):"+s2.contentEquals(sb1));
35 System.out.println("s2.equals(s22)"+s2.equals(s22));
36 //连接
37 System.out.println(s1.concat(s2));//相当于"+"
38 char[] c = new char[] {'a','e','i','h','h','j'};
39 String s4 = String.copyValueOf(c, 1, 3);
40 System.out.println(s4);
41 //数组化
42 byte[] b1 = s4.getBytes();
43 System.out.println(b1.toString());
44 s4.getChars(1, 2, c, 2);
45 System.out.println(c.toString());
46 System.out.println(s4.toCharArray());
47
48 for(byte cc:b1) {
49 System.out.print(cc + " ");
50 }
51 }
52
53 }
StringBuilder常用方法说明
1 package org.leo.demo.string;
2
3 public class TestStringBuilder {
4
5 public static void main(String[] args) {
6
7 StringBuilder sb = new StringBuilder();
8 System.out.println(sb.hashCode());
9 //增(追加)
10 sb.append("Java");
11 //增(插入)
12 sb.insert(0, "Hello ");
13 //改
14 sb.replace(5, 6, ",");
15 //删
16 sb.delete(5, 6);
17 System.out.println(sb);
18 //查
19 char c = sb.charAt(5);
20 System.out.println(c);
21 //反转
22 sb.reverse();
23 System.out.println(sb);
24 //长度及容量
25 System.out.println("sb.length():" + sb.length());
26 System.out.println("sb.capacity():" + sb.capacity());
27 //取子串
28 String string = sb.substring(2, 6);
29 System.out.println(string);
30 //改变长度,将保留前n的StringBuilder对象
31 sb.setLength(4);
32 System.out.println(sb);
33 System.out.println(sb.hashCode());
34 }
35
36 }