這篇文章主要介紹了JavaScript中操作字符串之localeCompare()方法的使用,是JS入門學習中的基礎知識,需要的朋友可以參考下
這個方法返回一個數字表示參考字符串是否到來之前或之后或相同的排序順序給定的字符串。
語法
- string.localeCompare( param )
下面是參數的詳細信息:
param : 字符串對象進行比較的字符串
返回值:
0 : 字符串匹配100%
1 : 不匹配,參數值來自于語言環境的排序順序字符串對象的值之前
-1 : 不匹配,參數值來自于語言環境的排序順序字符串對象的值之后
例子:
- <html>
- <head>
- <title>JavaScript String localeCompare() Method</title>
- </head>
- <body>
- <script type="text/javascript">
- var str1 = new String( "This is beautiful string" );
- var index = str1.localeCompare( "XYZ" );
- document.write("localeCompare first :" + index );
- document.write("<br />" );
- var index = str1.localeCompare( "AbCD ?" );
- document.write("localeCompare second :" + index );
- </script>
- </body>
- </html>
這將產生以下結果:
- localeCompare first :-1
- localeCompare second :1
新聞熱點
疑難解答
圖片精選