* Created by IntelliJ IDEA.<br>
* <b>User</b>: leizhimin<br>
* <b>Date</b>: 2008-6-2 22:14:16<br>
* <b>Note</b>: Please add comment here!
*/
public class StringTest {
public static void main(String args[]) {
//在池中和堆中分別創建String對象"abc",s1指向堆中對象
String s1 = new String("abc");
//s2直接指向池中對象"abc"
String s2 = "abc";
//在堆中新創建"abc"對象,s3指向該對象
String s3 = new String("abc");
//在池中創建對象"ab" 和 "c",並且s4指向池中對象"abc"
String s4 = "ab" + "c";
//c指向池中對象"c"
String c = "c";
//在堆中創建新的對象"abc",並且s5指向該對象
String s5 = "ab" + c;
String s6 = "ab".concat("c");
String s7 = "ab".concat(c);
System.out.println("------------實串-----------");
System.out.println(s1 == s2); //false
System.out.println(s1 == s3); //false
System.out.println(s2 == s3); //false
System.out.println(s2 == s4); //true
System.out.println(s2 == s5); //false
System.out.println(s2 == s6); //false
System.out.println(s2 == s7); //false
String b1 = new String("");
String b2 = "";
String b3 = new String("");
String b4 = "".intern();
String b5 = "" + "";
String b6 = "".concat("");
String b7 = " ".trim();
String b8 = " ";
String b9 = " ".trim();
System.out.println("------------空串-----------");
System.out.println(b1 == b2); //false
System.out.println(b1 == b3); //false
System.out.println(b2 == b3); //false
System.out.println(b2 == b4); //true
System.out.println(b2 == b5); //true*
System.out.println(b2 == b6); //true*
System.out.println(b2 == b7); //false*
System.out.println("-----a----");
System.out.println(b2.equals(b7)); //true
System.out.println(b7 == b8); //false
System.out.println(b7 == b9); //false
System.out.println(b7.equals(b9)); //true
System.out.println(b9 == null);//false
System.out.println("b8.trim():");
for (byte b : b8.getBytes()) {
System.out.print(">>>" + (int) b + " ");
}
System.out.println("\nb8.trim():");
for (byte b : b8.trim().getBytes()) {
System.out.print(">>>" + (int) b + " ");
}
System.out.println("\nb9.trim():");
for (byte b : b9.trim().getBytes()) {
System.out.print(">>>" + (int) b + " ");
}
}
}
/**
* 字符串轉碼測試
*
* @author leizhimin 2009-7-17 10:50:06
*/
public class TestEncoding {
public static void main(String[] args) throws UnsupportedEncodingException {
System.out.println("轉碼前,輸出Java系統屬性如下:");
System.out.println("user.country:" + System.getProperty("user.country"));
System.out.println("user.language:" + System.getProperty("user.language"));
System.out.println("sun.jnu.encoding:" + System.getProperty("sun.jnu.encoding"));
System.out.println("file.encoding:" + System.getProperty("file.encoding"));
System.out.println("---------------");
String s = "熔岩博客";
String s1 = new String(s.getBytes(), "UTF-8");
String s2 = new String(s.getBytes("UTF-8"), "UTF-8");
String s3 = new String(s.getBytes("UTF-8"));
String s4 = new String(s.getBytes("UTF-8"), "GBK");
String s5 = new String(s.getBytes("GBK"));
String s6 = new String(s.getBytes("GBK"), "GBK");
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
System.out.println(s4);
System.out.println(s5);
System.out.println(s6);
}
}
user.country:CN
user.language:zh
sun.jnu.encoding:GBK
file.encoding:UTF-8
---------------
熔岩博客
熔岩博客
熔岩博客
鐔斿博鍗氬
���Ҳ���
熔岩博客
Process finished with exit code 0
按字典順序比較兩個字符串。
compareToIgnoreCase(String str)
不考慮大小寫,按字典順序比較兩個字符串。