首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Java 字串判空與數字判斷最佳實務

     編輯:Java 相關知識

Java 字串安全判空與純數字檢查完整指南

字串處理是 Java 開發最高頻操作,錯誤的判空順序會導致 `NullPointerException`,數字檢查方式選擇影響效能與正確性。

1. 字串安全判空:順序決定生死

String selection = getInput();

// ✅ 正確:短路邏輯,先檢 null 再呼叫 equals
if (selection != null && !selection.isEmpty()) {
    // 安全使用 selection
}

// ❌ 錯誤:selection 為 null 時直接拋 NPE
if (!selection.isEmpty() && selection != null) { ... }

// Java 6+ 推薦用 isEmpty() 而非 equals(""),語意更清晰、效能略優
// Java 11+ 可用 isBlank() 同時排除空白字元
if (selection != null && !selection.isBlank()) { ... }

2. 判斷字串是否為純數字:三種方案對比

方案程式碼優點缺點
Character.isDigit 迴圈
public static boolean isNumeric(String s) {
    if (s == null || s.isEmpty()) return false;
    for (int i = 0; i < s.length(); i++) {
        if (!Character.isDigit(s.charAt(i))) return false;
    }
    return true;
}
無建立物件、效能最佳、無正則開銷僅支援 ASCII 0-9,不含負號、小數點、全形數字
正則表達式
public static boolean isNumeric(String s) {
    return s != null && s.matches("\\d+");
    // 或預編譯 Pattern: Pattern.compile("\\d+").matcher(s).matches();
}
語意清晰、可擴充 (如 `^-?\\d+(\\.\\d+)?$` 支援負數/小數)編譯/匹配有開銷、頻繁呼叫需快取 Pattern
ASCII 碼比對
public static boolean isNumeric(String s) {
    if (s == null) return false;
    for (char c : s.toCharArray()) {
        if (c < '0' || c > '9') return false;
    }
    return true;
}
原理直觀、無 API 依賴本質同方案一、易寫錯邊界

3. 進階需求:支援負號、小數、科學記號

// 使用 Java 內建解析器 (最可靠)
public static boolean isParsableNumber(String s) {
    if (s == null || s.isBlank()) return false;
    try {
        new java.math.BigDecimal(s); // 或 Double.parseDouble / Long.parseLong
        return true;
    } catch (NumberFormatException e) {
        return false;
    }
}

// 正則支援完整數字格式
private static final Pattern NUMBER_PATTERN = Pattern.compile(
    "^[-+]?(\\d+(\\.\\d*)?|\\.\\d+)([eE][-+]?\\d+)?$"
);
public static boolean isNumericRegex(String s) {
    return s != null && NUMBER_PATTERN.matcher(s).matches();
}

4. Apache Commons Lang / Guava 一行解決


// Commons Lang 3
StringUtils.isNumeric(CharSequence cs)        // 僅 0-9
StringUtils.isNumericSpace(CharSequence cs)   // 允許空白
NumberUtils.isCreatable(CharSequence cs)      // 支援負號、小數、0xFF、077 等

// Guava
CharMatcher.DIGIT.matchesAllOf(cs)            // 僅 0-9

最佳實務建議

  1. 判空永遠 `str != null && !str.isEmpty()` (或 `isBlank()`),順序不可顛倒。
  2. 高頻熱點路徑用 `Character.isDigit` 迴圈或預編譯 `Pattern`。
  3. 業務需求含負號/小數/千分位 → 用 `BigDecimal` 解析或完整正則。
  4. 專案已引入 Commons Lang/Guava → 直接用工具類,減少自造輪子。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved