Compares two strings: recognizes and handles embedded numbers. : String Compare « Data Type « Java






Compares two strings: recognizes and handles embedded numbers.

     

//package com.webex.ta.hydra.util;

import java.io.File;

/**
 * Created by Cisco WebEx.
 * User: vegaz
 * Date: 2010-9-28
 * Time: 11:58:01
 */
public class MiscUtils {
      /**
     * Compares two strings.<p>
     * <p/>
     * Unlike <function>String.compareTo()</function>,
     * this method correctly recognizes and handles embedded numbers.
     * For example, it places "My file 2" before "My file 10".<p>
     *
     * @param str1       The first string
     * @param str2       The second string
     * @param ignoreCase If true, case will be ignored
     * @return negative If str1 &lt; str2, 0 if both are the same,
     *         positive if str1 &gt; str2
     * @since jEdit 4.3pre5
     */
    public static int compareStrings(String str1, String str2, boolean ignoreCase) {
        char[] char1 = str1.toCharArray();
        char[] char2 = str2.toCharArray();

        int len = Math.min(char1.length, char2.length);

        for (int i = 0, j = 0; i < len && j < len; i++, j++) {
            char ch1 = char1[i];
            char ch2 = char2[j];
            if (Character.isDigit(ch1) && Character.isDigit(ch2)
                    && ch1 != '0' && ch2 != '0') {
                int _i = i + 1;
                int _j = j + 1;

                for (; _i < char1.length; _i++) {
                    if (!Character.isDigit(char1[_i])) {
                        //_i--;
                        break;
                    }
                }

                for (; _j < char2.length; _j++) {
                    if (!Character.isDigit(char2[_j])) {
                        //_j--;
                        break;
                    }
                }

                int len1 = _i - i;
                int len2 = _j - j;
                if (len1 > len2)
                    return 1;
                else if (len1 < len2)
                    return -1;
                else {
                    for (int k = 0; k < len1; k++) {
                        ch1 = char1[i + k];
                        ch2 = char2[j + k];
                        if (ch1 != ch2)
                            return ch1 - ch2;
                    }
                }

                i = _i - 1;
                j = _j - 1;
            } else {
                if (ignoreCase) {
                    ch1 = Character.toLowerCase(ch1);
                    ch2 = Character.toLowerCase(ch2);
                }

                if (ch1 != ch2)
                    return ch1 - ch2;
            }
        }

        return char1.length - char2.length;
    }
}

   
    
    
    
    
  








Related examples in the same category

1.How to compare String instances
2.String.compareTo
3.Check order of two strings
4.Check order of two strings ignoring case
5.Compare Strings
6.Comparing Strings
7.A string can be compared with a StringBuffer
8.Compares all Strings in an array and returns the index at which the Strings begin to differ.
9.Compares all Strings in an array and returns the initial sequence of characters that is common to all of them.
10.Compares two Strings, and returns the index at which the Strings begin to differ.
11.Compares two Strings, and returns the portion where they differ.
12.Compares two Strings, returning true if they are equal ignoring the case.
13.Compares two Strings, returning true if they are equal.
14.Compress 2 adjacent (single or double) quotes into a single (s or d) quote when found in the middle of a String.
15.Compute Levenshtein distance
16.Find the Levenshtein distance between two Strings.
17.Compare two String[] for differences, either may be null
18.count Occurrences
19.Returns the length of the longest shared prefix of the two input strings.
20.Returns the levenshtein distance of two strings