JavaScript文字列localeCompare()
例
「ab」と「cd」を比較します。
let text1 = "ab";
let text2 = "cd";
let result = text1.localeCompare(text2);
let text1 = "cd";
let text2 = "ab";
let result = text1.localeCompare(text2);
以下のその他の例。
定義と使用法
このlocaleCompare()
メソッドは、現在のロケールの2つの文字列を比較します。
このlocaleCompare()
メソッドは、ソート順-1、1、または0(前、後、または等しい場合)を返します。
現在のロケールは、ブラウザの言語設定に基づいています。
構文
string.localeCompare(compareString)
パラメーター
Parameter | Description |
compareString | Required. The string to compare with. |
戻り値
タイプ | 説明 |
数 | 3つの値のいずれか:文字列がcompareString の前にソートされている場合は-1 2つの文字列が等しい場合は0 、文字列がcompareString の後にソートされている場合は1 |
その他の例
let text1 = "ab";
let text2 = "ab";
let result = text1.localeCompare(text2);
let text1 = "A";
let text2 = "a";
let result = text1.localeCompare(text2);
ブラウザのサポート
localeCompare()
ECMAScript1(ES1)機能です。
ES1(JavaScript 1997)は、すべてのブラウザーで完全にサポートされています。
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes | Yes |