您的位置:首页技术文章
文章详情页

JAVA中文比较问题的分析解决

【字号: 日期:2024-06-11 16:52:57浏览:36作者:猪猪
内容: Java的中文问题由来已久,前不久笔者需要做内存中的中文比较排序,对字符串进行GBK或者GB2312编码以后,使用String.compareTo方法仍然不能得到正确结果。因此,怀着怀疑的态度,对JDK中String类的源代码做了一翻探究。(作者使用JDK为1.3.1版本) 以下是String.java中compareTo的源代码,请注意其中的注释: 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
标签: Java
相关文章: