localeCompare()方法2

2017-06-16 14:54:00
admin
原创
454
<!DOCTYPE html>
<html>
<head>
    <title>String Type localeCompare() Example</title>
    <script type="text/javascript">
    
        var stringValue = "yellow";       
        alert(stringValue.localeCompare("brick"));  //1
        alert(stringValue.localeCompare("yellow")); //0
        alert(stringValue.localeCompare("zoo"));    //-1
        
        //preferred technique for using localeCompare()
        function determineOrder(value) {
            var result = stringValue.localeCompare(value);
            if (result < 0){
                alert("The string 'yellow' comes before the string '" + value + "'.");
            } else if (result > 0) {
                alert("The string 'yellow' comes after the string '" + value + "'.");
            } else {
                alert("The string 'yellow' is equal to the string '" + value + "'.");
            }
        }
        
        determineOrder("brick");
        determineOrder("yellow");
        determineOrder("zoo");

    </script>
</head>
<body>

</body>
</html>
  • QQ讨论群
  • 关注微信
    • 欢迎咨询
    • 欢迎加入QQ群 16506021
  • 公众号:源宝网络