[英]How to compare string which contains integers in JAVA
I have some problem with comparing strings which contain integers.我在比较包含整数的字符串时遇到了一些问题。 Something like
A11
and A9
or BA230
and BA7
or 123
and 9
I know when I want to compare integers (which are string types), I need to pass into Integer and compare But this is not that case.像
A11
和A9
或BA230
和BA7
或123
和9
这样的东西我知道当我想比较整数(它们是字符串类型)时,我需要传入 Integer 并进行比较,但情况并非如此。
It also contains letters and numbers so I can't pass into Integer.它还包含字母和数字,所以我不能传入 Integer。
When I compare A11
and A9
using compareTo
method, it says A9
is bigger.当我使用
compareTo
方法比较A11
和A9
时,它说A9
更大。 And when I compare 123
with 9
it says 9
is bigger.当我将
123
与9
进行比较时,它说9
更大。
Has anyone faced this issue before?有没有人遇到过这个问题? Could you please help me?
请你帮助我好吗? Thanks.
谢谢。
/**
* 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();
}
For input "123abc", the method above will return 123.对于输入“123abc”,上述方法将返回 123。
For "abc1000def", 1000.对于“abc1000def”,为 1000。
For "555abc45", 555.对于“555abc45”,555。
For "abc", will return an empty string.对于“abc”,将返回一个空字符串。
// Then you can parse that to integer and then compare // 然后你可以把它解析成 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 is compare every character's unicode value one by one. String.compareTo 是逐一比较每个字符的 unicode 值。 it will return when find that two character not equal.
当发现两个字符不相等时,它将返回。
A11 compare to A9: A11 与 A9 的比较:
step1: 'A' compare to 'A'第 1 步:“A”与“A”比较
step2: '1' compare to '9'. step2:“1”与“9”比较。 '1' unicode value is 49, '9' unicode value is 57.
'1' unicode 值为 49,'9' unicode 值为 57。
So A9 is bigger.所以A9更大。
order_by is the same as above. order_by同上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.