当前页面: 开发资料首页 → Java 专题 → JAVA中文比较问题的分析解决
摘要: JAVA中文比较问题的分析解决
public class String
{
…
public int compareTo(String anotherString) {
int len1 = count;
int len2 = anotherString.count;
//n为两个字符串长度的最小者
int n = Math.min(len1, len2);
//获取字符数组
char v1[] = value;
char v2[] = anotherString.value;
//取偏依位置
/** The offset is the first index of the storage that is used. */
//offset 是第一个存储索引
int i = offset;
int j = anotherString.offset;
//如果i == j
//这里可能是判断取同一内存中两个字符串的情景。。。
// A <-- <----
// B s1 |
// C <-- |
// D s2
// E |
// F |
// G <----------
// 可能这种情况 i = j
if (i == j) {
int k = i;
int lim = n + i;
while (k < lim)
{
char c1 = v1[k];
char c2 = v2[k];
if (c1 != c2) file://直到找到一个不相等的字符,返回c1 - c2
return c1 - c2;
k++;
}
} else {
while (n-- != 0) file://直到两个字符串长度记数为0
{
char c1 = v1[i++]; file://分别取字符
char c2 = v2[j++];
if (c1 != c2) { //发现不相等,立即返回c1 - c2;
return c1 - c2;
}
}
}
return len1 - len2;
//最后这里可能出现的情况是: 两个字符串比较完之后还没有得到结果。相等的情况
}
…
}//end of class String
private static String __ENCODE__ = "GBK"; file://一定要是GBK
private static String __SERVER_ENCODE__ = "GB2312"; file://服务器上的缺省编码
/*
比较两字符串
*/
public int compare(String s1, String s2)
{
String m_s1 = null, m_s2 = null;
try
{
//先将两字符串编码成GBK
m_s1 = new String ( s1.getBytes(__SERVER_ENCODE__), __ENCODE__);
m_s2 = new String ( s2.getBytes(__SERVER_ENCODE__), __ENCODE__);
}
catch( Exception ex)
{
return s1.compareTo(s2);
}
int res = chineseCompareTo(m_s1, m_s2);
System.out.println("比较:" + s1 + " | " + s2 + "==== Result: " + res);
return res;
}
//获取一个汉字/字母的Char值
public static int getCharCode(String s)
{
if (s==null && s.equals(“”)) return -1; file://保护代码
byte [] b = s.getBytes();
int value = 0;
//保证取第一个字符(汉字或者英文)
for (int i = 0; i < b.length && i <= 2; i ++)
{
value = value * 100 + b[i];
}
return value;
}
//比较两个字符串
public int chineseCompareTo(String s1, String s2)
{
int len1 = s1.length();
int len2 = s2.length();
int n = Math.min(len1, len2);
for (int i = 0; i < n; i ++)
{
int s1_code = getCharCode(s1.charAt(i) + "");
int s2_code = getCharCode(s2.charAt(i) + "");
if (s1_code != s2_code) return s1_code - s2_code;
}
return len1 - len2;
}