String 的幾個 方法。 (java)

來源:互聯網
上載者:User

寫了幾年 java 程式,今天才看到 String 的 endwith startwith 。實在不好意思。
還有就是 不區分大小寫 字串比較。String.CASE_INSENSITIVE_ORDER

以後需要養成習慣,對於無論熟悉不熟悉的類(庫),時不時,多看兩眼方法。

import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;

public class StringUsage {

 // 幾個歐不太用的方法。
 public static void main(String[] args) {

  // 判斷字串首/尾。
  String contentS = "test String again. test ";
  System.out.println(contentS);
  System.out.println("contentS.startsWith(/"test/")/t"
    + contentS.startsWith("test"));
  System.out.println("contentS.endsWith(/"test/")/t"
    + contentS.endsWith("test"));

  // 忽略大小寫字串比較方法。
  System.out
    .println("contentS.compareToIgnoreCase(contentS.toUpperCase()))/t"
      + contentS.compareToIgnoreCase(contentS.toUpperCase()));

  // 不區分大小寫字串比較。
  Comparator<String> cmprStr = String.CASE_INSENSITIVE_ORDER;
  List<String> lstStr = new LinkedList<String>();
  // 準備資料。
  lstStr.add("test");
  lstStr.add("Test");
  lstStr.add("tEst");
  lstStr.add("rest");
  lstStr.add("sest");
  
  // 不區分大小寫排序。
  Collections.sort(lstStr, cmprStr);
  System.out.println(lstStr);
  // 區分大小寫排序。
  Collections.sort(lstStr);
  System.out.println(lstStr);

  System.out.println("/ntest String.valueOf()");
  
  // 用 valueOf 處理 null 對象的字串轉換。
  try {
   String ss = null;
   System.out.println(ss);
   System.out.println(String.valueOf(null)); // 比較奇怪的地方
  } catch (RuntimeException e1) {
      e1.printStackTrace();
  }
  
  BigDecimal db = null;
  try {
   System.out.println(((BigDecimal)db).toString());
  } catch (Exception e) {
   e.printStackTrace();
  }
  try {
   System.out.println(String.valueOf(db));
  } catch (Exception e) {
   e.printStackTrace();
  }
  db = new BigDecimal("100.00");
  try {
   System.out.println(((BigDecimal)db).toString());
  } catch (Exception e) {
   e.printStackTrace();
  }

 }

}

運行結果:

test String again. test
contentS.startsWith("test") true
contentS.endsWith("test") false
contentS.compareToIgnoreCase(contentS.toUpperCase())) 0
[rest, sest, test, Test, tEst]
[Test, rest, sest, tEst, test]

test String.valueOf()
null
java.lang.NullPointerException
 at java.lang.String.<init>(Unknown Source)
 at java.lang.String.valueOf(Unknown Source)
 at test.StringUsage.main(StringUsage.java:50)
null
java.lang.NullPointerException
 at test.StringUsage.main(StringUsage.java:58)
100.00

 

 

20060312

比較有趣的情況是 直接 傳 null 給 valueOf 。會拋null 指標錯。看 String 的源碼。估計是此時 調用的是

    public static String valueOf(char data[]) {
      return new String(data);
    }

異常是從

    public String(char value[]) {
        int size = value.length;
        char[] v = new char[size];
        System.arraycopy(value, 0, v, 0, size);
        this.offset = 0;
        this.count = size;
        this.value = v;
    }

裡的第一句出來的。

沒有走

    public static String valueOf(Object obj) {
        return (obj == null) ? "null" : obj.toString();
    }

這是為什麼呢。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.