最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Ordinal string compare in JavaScript? - Stack Overflow

programmeradmin2浏览0评论

In javascript:

"Id".localeCompare("id")

will report that "id" is bigger. I want to do ordinal (not locale) pare such that "Id" is bigger. This is similar to String.CompareOrdinal in C#. How can I do it?

In javascript:

"Id".localeCompare("id")

will report that "id" is bigger. I want to do ordinal (not locale) pare such that "Id" is bigger. This is similar to String.CompareOrdinal in C#. How can I do it?

Share Improve this question edited May 17, 2012 at 17:53 user166390 asked May 17, 2012 at 13:41 Yaron NavehYaron Naveh 24.4k33 gold badges106 silver badges164 bronze badges 1
  • 4 Um, just use the regular > and < operators? (By the way, in an ordinal pare, il is bigger than Id because i is U+0069 and I is U+0049). – Raymond Chen Commented May 17, 2012 at 13:51
Add a ment  | 

5 Answers 5

Reset to default 5

I support the answers given by Raymond Chen and pst. I will back them up with documentation from my favorite site for answers to JavaScript questions -- The Mozilla Developer Network. As an aside, I would highly remend this site for any future JavaScript questions you may have.

Now, if you go to the MDN section entitled String, under the section "Comparing strings", you will find this description:

C developers have the strcmp() function for paring strings. In JavaScript, you just use the less-than and greater-than operators:

var a = "a";  
var b = "b";  
if (a < b) // true  
  print(a + " is less than " + b);  
else if (a > b)  
  print(a + " is greater than " + b);  
else  
  print(a + " and " + b + " are equal.");

A similar result can be achieved using the localeCompare method inherited by String instances.

If we were to use the string "Id" for a and "id" for b then we would get the following result:

"Id is less than id"

This is the same result that Yaron got earlier when using the localeCompare method. As noted in MDN, using the less-than and greater-than operators yields similar results as using localeCompare.

Therefore, the answer to Yaron's question is to use the less-than (<) and greater-than (>) operators to do an ordinal parison of strings in JavaScript.

Since Yaron mentioned the C# method String.CompareOrdinal, I would like to point out that this method produces exactly the same results as the above JavaScript. According to the MSDN C# documentation, the String.CompareOrdinal(String, String) method "Compares two specified String objects by evaluating the numeric values of the corresponding Char objects in each string." So the two String parameters are pared using the numeric (ASCII) values of the individual characters.

If we use the original example by Yaron Naveh in C#, we have:

int result = String.CompareOrdinal("Id", "id");

The value of result is an int that is less than zero, and is probably -32 because the difference between "I" (0x49) and "i" (0x69) is -0x20 = -32. So, lexically "Id" is less than "id", which is the same result we got earlier.

As Raymond noted (and explained) in a ment, an "ordinal" non-locale aware pare is as simple as using the various equality operators on strings (just make sure both operands are strings):

"a" > "b" // false 
"b" > "a" // true

To get a little fancy (or don't muck with [[prototype]], the function is the same):

String.prototype.pare = function (a, b) {
    return ((a == b ? 0)
        ? (a > b : 1)
        : -1)
}

Then:

"a".pare("b") // -1

Happy coding.

Just a guess: by inverting case on all letters?

function pareOrdinal(ori,des){
    for(var index=0;index<ori.length&&index<des.length;index++){
        if(des[index].charCodeAt(0)<ori[index].charCodeAt(0)){
            return -1;
            break;
        }
    }
    if(parseInt(index)===des.length-1){
        return 0;
    }
    return 1;
}

pareOrdinal("idd","id");//output 1

if you need to pare and find difference between two string, please check this:

function findMissingString() {
    var str1 = arguments[0];
    var str2 = arguments[1];
    var i = 0 ;
    var j = 0 ;
    var text = '' ;
    while(i != (str1.length >= str2.length ? str1.length : str2.length )) {
        if(str1.charAt(i) == str2.charAt(j)) {
            i+=1 ;
            j+=1;
        } else {
            var indexing =  (str1.length >= str2.length ? str1.charAt(i) : str2.charAt(j));
            text = text + indexing ;
            i+=1;
            j+=1;
        }
    }
    console.log("From Text = " + text);
}
findMissingString("Hello","Hello world");

发布评论

评论列表(0)

  1. 暂无评论