Coffeescript-string-localecompare

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

CoffeeScript文字列-localeCompare()

説明

このメソッドは文字列を受け取り、それを呼び出し元のStringオブジェクトと比較します。 両方が等しい場合、0を返します。それ以外の場合は、-1または1を返します。 また、パラメーターとして渡された文字列が、ローカルブラウザー言語に従ってソートされた順序で最初に来る場合、1を返します。呼び出し文字列がソートされた順序で最初に来る場合、-1が返されます。

構文

以下に、JavaScriptの* localeCompare()*メソッドの構文を示します。 CoffeeScriptコードから同じメソッドを使用できます。

string.localeCompare( param )

次の例は、CoffeeScriptコードでJavaScriptのlocaleCompare()メソッドを使用する方法を示しています。 このコードを string_localecompare.coffee という名前のファイルに保存します

str1 = "This is beautiful string"
str2 = "This is beautiful string"
str3 = "abcd"
str4 = "xyz"
console.log "The value of str1:: "+str1
console.log "The value of str2:: "+str2
console.log "The value of str3:: "+str3
console.log "comparing the strings str1 and str2 ::"

index = str1.localeCompare str2
switch index
   when 0 then console.log "Both strings are equal"
   when 1 then console.log "Both strings are not equal and the string passed as parameter will be first in the sorted order."
   when -1 then console.log "Both strings are not equal and the calling string object will be first in the sorted order."


console.log "comparing the strings str1 and str3 ::"
index = str1.localeCompare str3
switch index
   when 0 then console.log "Both strings are equal"
   when 1 then console.log "Both strings are not equal and the string passed as parameter will be first in the sorted order."
   when -1 then console.log "Both strings are not equal and the calling string object will be first in the sorted order."

console.log "comparing the strings str1 and str4 ::"
index = str1.localeCompare str4
index = str1.localeCompare str3
switch index
   when 0 then console.log "Both strings are equal"
   when 1 then console.log "Both strings are not equal and the string passed as parameter will be first in the sorted order."
   when -1 then console.log "Both strings are not equal and the calling string object will be first in the sorted order."
  • コマンドプロンプト*を開き、以下に示すように.coffeeファイルをコンパイルします。
c:\> coffee -c string_localecompare.coffee

コンパイル時に、次のJavaScriptが提供されます。

//Generated by CoffeeScript 1.10.0
(function() {
  var index, str1, str2, str3, str4;

  str1 = "This is beautiful string";

  str2 = "This is beautiful string";

  str3 = "abcd";

  str4 = "xyz";

  console.log("The value of str1:: " + str1);

  console.log("The value of str2:: " + str2);

  console.log("The value of str3:: " + str3);

  console.log("comparing the strings str1 and str2 ::");

  index = str1.localeCompare(str2);

  switch (index) {
    case 0:
      console.log("Both strings are equal");
      break;
    case 1:
      console.log("Both strings are not equal and the string passed as parameter will be first in the sorted order.");
      break;
    case -1:
      console.log("Both strings are not equal and the calling string object will be first in the sorted order.");
  }

  console.log("comparing the strings str1 and str3 ::");

  index = str1.localeCompare(str3);

  switch (index) {
    case 0:
      console.log("Both strings are equal");
      break;
    case 1:
      console.log("Both strings are not equal and the string passed as parameter will be first in the sorted order.");
      break;
    case -1:
      console.log("Both strings are not equal and the calling string object will be first in the sorted order.");
  }

  console.log("comparing the strings str1 and str4 ::");

  index = str1.localeCompare(str4);

  index = str1.localeCompare(str3);

  switch (index) {
    case 0:
      console.log("Both strings are equal");
      break;
    case 1:
      console.log("Both strings are not equal and the string passed as parameter will be first in the sorted order.");
      break;
    case -1:
      console.log("Both strings are not equal and the calling string object will be first in the sorted order.");
  }

}).call(this);

次に、*コマンドプロンプト*を再度開き、以下に示すようにCoffeeScriptファイルを実行します。

c:\> coffee string_localecompare.coffee

CoffeeScriptファイルを実行すると、次の出力が生成されます。

The value of str1:: This is beautiful string
The value of str2:: This is beautiful string
The value of str3:: abcd
comparing the strings str1 and str2 ::
Both strings are equal
comparing the strings str1 and str3 ::
Both strings are not equal and the string passed as parameter will be first in the sorted order.
comparing the strings str1 and str4 ::
Both strings are not equal and the string passed as parameter will be first in the sorted order.