Chaîne CoffeeScript - localeCompare ()
La description
Cette méthode accepte une chaîne et la compare avec l'objet String appelant. Si les deux sont égaux, il renvoie 0; sinon, il renvoie -1 ou 1. Et si la chaîne passée en paramètre vient en premier dans l'ordre trié selon la langue du navigateur local, il renvoie 1; et si la chaîne appelante vient en premier dans l'ordre trié, -1 est renvoyé.
Syntaxe
Voici la syntaxe de localeCompare()méthode de JavaScript. Nous pouvons utiliser la même méthode à partir du code CoffeeScript.
string.localeCompare( param )
Exemple
L'exemple suivant illustre l'utilisation de la méthode localeCompare () de JavaScript dans le code CoffeeScript. Enregistrez ce code dans un fichier avec un nomstring_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."
Ouvrez le command prompt et compilez le fichier .coffee comme indiqué ci-dessous.
c:\> coffee -c string_localecompare.coffee
Lors de la compilation, il vous donne le JavaScript suivant.
// 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);
Maintenant, ouvrez le command prompt à nouveau et exécutez le fichier CoffeeScript comme indiqué ci-dessous.
c:\> coffee string_localecompare.coffee
Lors de l'exécution, le fichier CoffeeScript produit la sortie suivante.
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.