Java比较字符串大小

时间:2022-06-29 16:16:25
/**
* 比较字符串的大小
*
* @param paramString1
* @param paramString2
* @return true paramString1 < paramString2;
* <p/>
* false paramString1 >= paramString2
*/
private static boolean compareTheSize(String paramString1, String paramString2) {
boolean flag = false;
char[] str1Chars = paramString1.toCharArray();
char[] str2Chars = paramString2.toCharArray();
int num = str1Chars.length < str2Chars.length ? str1Chars.length : str2Chars.length;
for (int i = 0; i < num; i++) {
if (str1Chars[i] > str2Chars[i]) {
break;
} else if (str1Chars[i] < str2Chars[i]) {
flag = true;
break;
} else if (i == num - 1) {
flag = str1Chars.length < str2Chars.length;
}
}
return flag;
}

/**
* 比较字符串的大小
*
* @param paramString1
* @param paramString2
* @return true paramString1大于paramString2; false paramString1不大于paramString2
*/

private static boolean compareTheSize2(String paramString1, String paramString2) {
boolean flag = false;
char[] str1Chars = paramString1.toCharArray();
char[] str2Chars = paramString2.toCharArray();
int num = str1Chars.length < str2Chars.length ? str1Chars.length : str2Chars.length;
for (int i = 0; i < num; i++) {
if (str1Chars[i] > str2Chars[i]) {
flag = true;
break;
} else if (str1Chars[i] < str2Chars[i]) {
break;
} else if (i == num - 1) {
flag = str1Chars.length > str2Chars.length;
}
}
return flag;
}