簡體   English   中英

如何比較 JAVA 中包含整數的字符串

[英]How to compare string which contains integers in JAVA

我在比較包含整數的字符串時遇到了一些問題。 A11A9BA230BA71239這樣的東西我知道當我想比較整數(它們是字符串類型)時,我需要傳入 Integer 並進行比較,但情況並非如此。

它還包含字母和數字,所以我不能傳入 Integer。

當我使用compareTo方法比較A11A9時,它說A9更大。 當我將1239進行比較時,它說9更大。

有沒有人遇到過這個問題? 請你幫助我好嗎? 謝謝。

/**
 * Similar to compareTo method But compareTo doesn't return correct result for string+integer strings something like `A11` and `A9`
  */

private int newCompareTo(String comp1, String comp2) {
    // If any value has 0 length it means other value is bigger
    if (comp1.length() == 0) {
        if (comp2.length() == 0) {
            return 0;
        }
        return -1;
    } else if (comp2.length() == 0) {
        return 1;
    }
    // Check if first string is digit
    if (TextUtils.isDigitsOnly(comp1)) {
        int val1 = Integer.parseInt(comp1);
        // Check if second string is digit
        if (TextUtils.isDigitsOnly(comp2)) { // If both strings are digits then we only need to use Integer compare method
            int val2 = Integer.parseInt(comp2);
            return Integer.compare(val1, val2);
        } else { // If only first string is digit we only need to use String compareTo method
            return comp1.compareTo(comp2);
        }

    } else { // If both strings are not digits

        int minVal = Math.min(comp1.length(), comp2.length()), sameCount = 0;

        // Loop through two strings and check how many strings are same
        for (int i = 0;i < minVal;i++) {
            char leftVal = comp1.charAt(i), rightVal = comp2.charAt(i);
            if (leftVal == rightVal) {
                sameCount++;
            } else {
                break;
            }
        }
        if (sameCount == 0) {
            // If there's no same letter, then use String compareTo method
            return comp1.compareTo(comp2);
        } else {
            // slice same string from both strings
            String newStr1 = comp1.substring(sameCount), newStr2 = comp2.substring(sameCount);
            if (TextUtils.isDigitsOnly(newStr1) && TextUtils.isDigitsOnly(newStr2)) { // If both sliced strings are digits then use Integer compare method
                return Integer.compare(Integer.parseInt(newStr1), Integer.parseInt(newStr2));
            } else { // If not, use String compareTo method
                return comp1.compareTo(comp2);
            }
        }
    }
}
public static String extractNumber(final String str) {                

if(str == null || str.isEmpty()) return "";

StringBuilder sb = new StringBuilder();
boolean found = false;
for(char c : str.toCharArray()){
    if(Character.isDigit(c)){
        sb.append(c);
        found = true;
    } else if(found){
        // If we already found a digit before and this char is not a digit, stop looping
        break;                
    }
}

return sb.toString();
}

對於輸入“123abc”,上述方法將返回 123。

對於“abc1000def”,為 1000。

對於“555abc45”,555。

對於“abc”,將返回一個空字符串。

// 然后你可以把它解析成 integer 然后比較

public int compareTo(String anotherString) {
        int len1 = value.length;
        int len2 = anotherString.value.length;
        int lim = Math.min(len1, len2);
        char v1[] = value;
        char v2[] = anotherString.value;

        int k = 0;
        while (k < lim) {
            char c1 = v1[k];
            char c2 = v2[k];
            if (c1 != c2) {
                return c1 - c2;
            }
            k++;
        }
        return len1 - len2;
    }

String.compareTo 是逐一比較每個字符的 unicode 值。 當發現兩個字符不相等時,它將返回。

A11 與 A9 的比較:

第 1 步:“A”與“A”比較

step2:“1”與“9”比較。 '1' unicode 值為 49,'9' unicode 值為 57。

所以A9更大。

order_by同上。

order_by

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM