1、借助JDK,选取几个String与StringBuffer、StringBuilder的常用API,并编写实例测试API的功能。
String常用API
1 /**JDK中String 常用API equals 源码 2 public boolean equals(Object anObject) { 3 if (this == anObject) { 4 return true; 5 } 6 if (anObject instanceof String) { 7 String anotherString = (String)anObject; 8 int n = value.length; 9 if (n == anotherString.value.length) { 10 char v1[] = value; 11 char v2[] = anotherString.value; 12 int i = 0; 13 while (n-- != 0) { 14 if (v1[i] != v2[i]) 15 return false; 16 i++; 17 } 18 return true; 19 } 20 } 21 return false; 22 } 23 **/ 24 25 String a="123"; 26 String b="123"; 27 System.out.println(a.equals(b));
StringBuffer常用API
1 /** 2 1. append(参数); 向StringBuffer对象后追加任意类型的数据,返回StringBuffer。它有多个重载方法,可以追加int string char object array等 3 **/ 4 5 StringBuffer buffer = new StringBuffer(); 6 7 buffer.append(89); 8 9 10 /** 11 2. capacity(); 返回int capacity,即缓冲区的容量大小 12 **/ 13 14 StringBuffer buffer = new StringBuffer("abc"); 15 16 17 18 buffer.capacity(); 19 20 //返回19; 原本16的容量加上字符串长度 21 22 23 24 //3. charAt(int index); 返回缓冲区指定索引处的char字符 25 26 StringBuffer buffer = new StringBuffer("abc"); 27 28 buffer.charAt(1);//取得b
StringBuilder常用API
1 //创建StringBuilder对象 2 StringBuilder bu = new StringBuilder(); 3 //使用append方法添加数据 4 //注意:append方法返回的是this自身,无需接收返回值 5 bu.append("abc"); 6 bu.append(111); 7 bu.append(true) 8 bu.append('中'); 9 bu.append(3.1415); 10 System.out.println(bu);// abc111true中3.1415 11 12 //链式编程:方法返回值是一个对象,可以继续调用方法 13 bu.append("abc").append(111).append(true).append('中').append(3.1415); 14 System.out.println(bu);//abc111true中3.1415
2、请简述String,StringBuffer,StringBuilder三者之间的共同点与区别,应该分别在何种场景下使用。

3、为什么不建议在for循环中使用“+”进行字符串拼接?
效率低下,需要一直创建新的对象,浪费大量空间资源且费时。
4、什么是字符串的编码与解码?
编码:将Unicode字符集转换为本地字符集(GB2312或GBK)的过程
1 import java.io.*; 2 public class CharCode { 3 public static void printByteArray(String msg,byte[] t){ 4 System.out.println(msg+"****************"); 5 for(int i=0;i<t.length;i++){ 6 System.out.println(Integer.toHexString(t[i])); } 7 } 8 public static void printCharArray(String msg,char[] c){ 9 System.out.println(msg+"****************"); 10 for(int i=0;i<c.length;i++){ 11 System.out.println(Integer.toHexString(c[i])); } 12 } 13 14 public static void main(String[] args){ 15 try{ 16 String str = "中文"; 17 System.out.println(str); 18 printCharArray("unicode:",str.toCharArray()); /*unicode字符集中 19 对"中文"二字的对应代码 */ 20 byte[] b =str.getBytes(“GB2312”); /*编码:转为本地字符集 21 GBK2312对应的代码 */ 22 printByteArray("GB2312",b); 23 byte[] m =str.getBytes("ISO8859-1"); //转为ISO8859-1对应的代 24 //码 25 printByteArray("ISO8859-1",m); /* ISO8859-1是英文字符集,没 26 有对应的汉字代码,所以转化错误 */ 27 } 28 catch(UnsupportedEncodingException e){ 29 System.out.println("没有相应的字符集!"); 30 } 31 } 32 }

解码:将本地字符集转换为Unicode字符集的过程
1 public static void main(String[] args){ 2 byte[] b = new byte[6]; 3 int t=0,pos=0; 4 String s; 5 try{ 6 while(t!='\n'){ 7 t=System.in.read(); 8 b[pos]=(byte)t; 9 pos++; 10 } 11 printByteArray("本地码:",b); 12 s = new String(b,“GBK”); //解码 13 System.out.println(s); 14 printCharArray("unicode码:",s.toCharArray()); 15 } 16 catch (Exception e){ 17 System.out.println(e.getMessage()); 18 } 19 }

浙公网安备 33010602011771号