String compare Case Sensitive - Node.js String

Node.js examples for String:Case

Description

String compare Case Sensitive

Demo Code


String.prototype.compare = function compareStrings(other, caseSensitive) {
    if (caseSensitive){
        return compareCaseSensitive(this, other);
    } /* ww w .j a v a2 s.  c o m*/
    else{
        return compareCaseInsesitive(this, other);
    } 

    function compareCaseSensitive(str1, str2) {
        if (str1.length != str2.length) {
            return false;
        }
        for (var i = 0; i < str1.length; i += 1) {
            if (str1[i] !== str2[i]) {
                return false;
            }
        }
        return true;
    }

    function compareCaseInsesitive(str1, str2) {
        return compareCaseSensitive(str1.toLowerCase(), str2.toLowerCase());
    }
}

var a = "a";
var A = "a"
console.log(a.compare(A,true));

Related Tutorials