繁体   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