实例代码“Ctrl+/”提示“F11/ESC”全屏 返回 格式化 恢复 运行
x
 
1
<!DOCTYPE html>
2
<html>
3
<head>
4
    <title>String Type localeCompare() Example</title>
5
    <script type="text/javascript">
6
    
7
        var stringValue = "yellow";       
8
        alert(stringValue.localeCompare("brick"));  //1
9
        alert(stringValue.localeCompare("yellow")); //0
10
        alert(stringValue.localeCompare("zoo"));    //-1
11
        
12
        //preferred technique for using localeCompare()
13
        function determineOrder(value) {
14
            var result = stringValue.localeCompare(value);
15
            if (result < 0){
16
                alert("The string 'yellow' comes before the string '" + value + "'.");
17
            } else if (result > 0) {
18
                alert("The string 'yellow' comes after the string '" + value + "'.");
19
            } else {
20
                alert("The string 'yellow' is equal to the string '" + value + "'.");
21
            }
22
        }
23
        
24
        determineOrder("brick");
25
        determineOrder("yellow");
26
        determineOrder("zoo");
27
28
    </script>
29
</head>
30
<body>
31
32
</body>
33
</html>