CoffeeScript String - localeCompare ()

Descrição

Este método aceita uma string e a compara com o objeto String de chamada. Se ambos forem iguais, ele retorna 0; caso contrário, retorna -1 ou 1. E se a string passada como parâmetro vier primeiro na ordem de classificação de acordo com o idioma do navegador local, ela retornará 1; e se a string de chamada vier primeiro na ordem de classificação, -1 será retornado.

Sintaxe

Dada a seguir é a sintaxe de localeCompare()método de JavaScript. Podemos usar o mesmo método do código CoffeeScript.

string.localeCompare( param )

Exemplo

O exemplo a seguir demonstra o uso do método localeCompare () de JavaScript no código CoffeeScript. Salve este código em um arquivo com o nomestring_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."

Abra o command prompt e compilar o arquivo .coffee conforme mostrado abaixo.

c:\> coffee -c string_localecompare.coffee

Na compilação, ele fornece o seguinte 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);

Agora, abra o command prompt novamente e execute o arquivo CoffeeScript conforme mostrado abaixo.

c:\> coffee string_localecompare.coffee

Ao ser executado, o arquivo CoffeeScript produz a seguinte saída.

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.