Javascript-string-localecompare

提供:Dev Guides
移動先:案内検索

JavaScript文字列-localeCompare()メソッド

説明

このメソッドは、参照文字列がソート順で指定された文字列の前後にあるか、同じかを示す数値を返します。

構文

localeCompare()メソッドの構文は次のとおりです-

string.localeCompare( param )

引数の詳細

*param* -_string_オブジェクトと比較される文字列。

戻り値

  • 0 -文字列が100%に一致する場合。
  • 1 -一致なし、パラメータ値はロケールのソート順で_string_オブジェクトの値の前に来る
  • -1 -一致せず、パラメータ値はローカルソート順で_string_オブジェクトの値の後に来る

次の例を試してください。

<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