Java String base of comparison

2010-03-29  来源:本站原创  分类:Java  人气:232 

Java6 the standard library inside the string comparison methods, feeling some of them are redundant. In other words, two Byte comparing the Character.toUpperCase (c1), after, if not equal, it should be these two char is not the case, and there is no need and then compare Character.toLowerCase (c1) of the.

public int compare(String s1, String s2) {
            int n1=s1.length(), n2=s2.length();
            for (int i1=0, i2=0; i1<n1 && i2<n2; i1++, i2++) {
                char c1 = s1.charAt(i1);
                char c2 = s2.charAt(i2);
                if (c1 != c2) {
                    c1 = Character.toUpperCase(c1);
                    c2 = Character.toUpperCase(c2);
                    if (c1 != c2) {
                        c1 = Character.toLowerCase(c1);
                        c2 = Character.toLowerCase(c2);
                        if (c1 != c2) {
                            return c1 - c2;
                        }
                    }
                }
            }
            return n1 - n2;
        }
相关文章
  • Java String base of comparison 2010-03-29

    Java6 the standard library inside the string comparison methods, feeling some of them are redundant. In other words, two Byte comparing the Character.toUpperCase (c1), after, if not equal, it should be these two char is not the case, and there is no

  • C / C + + & Java string comparison splicing efficiency 2011-01-09

    C / C + + & Java string comparison splicing efficiency C / C + + splicing as a string of many, especially the so-called "VC + +" language (should not be called the language, but many use the VC + + programmers thrown it into a VC + + languag

  • C / C + + & Java string comparison splicing efficiency (lower) [content over the length is truncated to complete documentation and source code can be downloaded] 2011-01-10

    C / C + + string stitching test run results: Release version. TEST_TIME: 90000 C language memcpy append Used: 0 ms. string length: 630000 std:: string + = operator Used: 15 ms. string length: 630000 std:: string append Used: 16 ms. string length: 630

  • C / C + + & Java string splicing efficiency of contrast (upper) [content over the length is truncated to complete documentation and source code can be downloaded] 2011-01-09

    C / C + + & Java string comparison splicing efficiency C / C + + splicing as a string of many, especially the so-called "VC + +" language (should not be called the language, but many use the VC + + programmers thrown it into a VC + + languag

  • Java String object on a small summary in the 2011-05-05

    String is a pair of double quotes character sequence, in the JAVA language, the string data is actually achieved by the String class. Java string class is divided into two types: one is the change in the program will not be the same length of string;

  • Java string to compare the size of the date and time 2011-05-08

    Method One: DateFormat fmt = new SimpleDateFormat ("yyyy-MM-dd"); String dateBegin = fmt.format (carrierCommand.getDateBegin ()); String dateEnd = fmt.format (carrierCommand.getDateEnd ()); / / If the date format is not available '2008-05-22 ',

  • java string encoding conversion 2010-03-29

    java string encoding conversion, whether the program localization is international, will involve the conversion of character encoding issues. Especially in web applications often need to deal with Chinese characters, when a string is needed, encoding

  • java string remove spaces. Enter. linefeed. Tab 2008-11-21

    java string remove spaces, carriage returns, line breaks, tabs, this method uses a regular expression java public class StringUtil ( public static void replaceBlank () ( Pattern p = Pattern.compile ( "\ \ s * | \ t | \ R | \ N"); String str = &q

  • Diagram to show java string's immutability 2010-07-06

    Diagram to show java string's immutability

  • [Transfer] java string to determine whether the number in the three methods 2010-10-29

    java string to determine whether the numbers in the three methods A function that comes with JAVA public static boolean isNumeric (String str) { for (int i = str.length ();-- i> = 0;) { if (! Character.isDigit (str.charAt (i))) { return false; } } re

  • About java String split 2011-05-08

    java String split deal with the problem public static void main (String args []) { String s = "a.txt"; String a [] = s.split ("."); System.out.println (a.length); } The output is 0 is not 2? Best answer . In the regular expression whic

  • java string 转date方法如何实现 2013-11-16

    在开发应用中经常会使用到java string 转date这种不是很常见的做法,本文将以此问题提供详细解决方案,需要了解的朋友可以参考下 针对JSON 返回String 类型 两次格式化就行了,例如: Java代码 String s = "2012-08-25"; SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat sdf2 = new SimpleDateForm

  • Java String字符串和Unicode字符相互转换代码 2014-09-25

    这篇文章主要介绍了Java String字符串和Unicode字符相互转换代码,需要的朋友可以参考下 java环境安装后jdk的bin目录有个native2ascii.exe可以实现类似的功能,但是通过java代码也可以实现同样的功能. 字符串转换unicode java方法代码片段: /** * 字符串转换unicode */ public static String string2Unicode(String string) { StringBuffer unicode = new Stri

  • java string类的常用方法详细介绍 2015-01-05

    在开发过程中经常会使用到java string类的方法,本文将以此问题进行详细介绍 String : 字符串类型 一.构造函数 String(byte[ ] bytes):通过byte数组构造字符串对象. String(char[ ] value):通过char数组构造字符串对象. String(Sting original):构造一个original的副本.即:拷贝一个original. String(StringBuffer buffer):通过StringBuffer数组构造字符串对象.

  • Java String 字符串 2014-06-25

    转载自:http://my.codeweblog.com/brucelee80/blog String String是引用类型而非基本数据类型,代表不可变的字符序列. String对象的创建,比如: String str1 = new String("Java"); // 其他构造方法请参考API 其中的"Java"是字面量(直接量),上面的语句实际创建了两个String对象,一个是"Java"对象,存储在常量池中,一个是new时在堆中分配的内存

  • java:String类hashCode()的实现 2014-10-30

    一个对象只有一个hashcode,多个对象的hashcode可能相同. 源代码位置:java-source/java/lang/String.java,hashCode()方法: public int hashCode() { int h = hash; if (h == 0 && value.length > 0) { char val[] = value; for (int i = 0; i < value.length; i++) { h = 31 * h + val[i

  • Java.String(学习笔记) 2015-01-08

    学习Java时的代码笔记,排版可能有些问题,代码拿来直接就可以用,毕竟全手打,难免会有错,欢迎指正 /* String类 字符串是一个特殊的对象 字符串一旦初始化就不可以被改变 String str = "abc"; String str1 = new String("abc"); 有什么区别? */ class StringMainDemo1 { public static void main(String[] args) { String s = new Str

  • JAVA String.format 方法使用介绍 2015-04-23

    JAVA String.format 方法使用介绍 1.对整数进行格式化:%[index$][标识][最小宽度]转换方式 我们可以看到,格式化字符串由4部分组成,其中%[index$]的含义我们上面已经讲过,[最小宽度]的含义也很好理解,就是最终该整数转化的字符串最少包含多少位数字.我们来看看剩下2个部分的含义吧: 标识: '-' 在最小宽度内左对齐,不可以与"用0填充"同时使用 '#' 只适用于8进制和16进制,8进制时在结果前面增加一个0,16进制时在结果前面增加0x '+' 结果

  • A classic problem of Java String objects (new String ()) 2010-03-29

    First look at an example, the code is as follows: public class Test { public static void main(String[] args) { String str = "abc"; String str1 = "abc"; String str2 = new String("abc"); System.out.println(str == str1); System.

  • JAVA String.format method to use Introduction 2010-01-16

    In JDK1.5 in, String class adds a very useful function of the static format (String format, Objece ... argues), types of data can be formatted as a string and output. Which format parameter specifies the output format is the most complex and most dif