
- CoffeeScript Tutorial
- CoffeeScript - Home
- CoffeeScript - Overview
- CoffeeScript - Environment
- CoffeeScript - command-line utility
- CoffeeScript - Syntax
- CoffeeScript - Data Types
- CoffeeScript - Variables
- CoffeeScript - Operators and Aliases
- CoffeeScript - Conditionals
- CoffeeScript - Loops
- CoffeeScript - Comprehensions
- CoffeeScript - Functions
- CoffeeScript Object Oriented
- CoffeeScript - Strings
- CoffeeScript - Arrays
- CoffeeScript - Objects
- CoffeeScript - Ranges
- CoffeeScript - Splat
- CoffeeScript - Date
- CoffeeScript - Math
- CoffeeScript - Exception Handling
- CoffeeScript - Regular Expressions
- CoffeeScript - Classes and Inheritance
- CoffeeScript Advanced
- CoffeeScript - Ajax
- CoffeeScript - jQuery
- CoffeeScript - MongoDB
- CoffeeScript - SQLite
- CoffeeScript Useful Resources
- CoffeeScript - Quick Guide
- CoffeeScript - Useful Resources
- CoffeeScript - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
CoffeeScript String - localeCompare()
Description
This method accepts a string and compares it with the calling String object. If both are equal, it returns 0; else it returns -1 or 1. And if the string passed as parameter comes first in the sorted order according to local browser language, it returns 1; and if the calling string comes first in the sorted order, -1 is returned.
Syntax
Given below is the syntax of localeCompare() method of JavaScript. We can use the same method from the CoffeeScript code.
string.localeCompare( param )
Example
The following example demonstrates the usage of localeCompare() method of JavaScript in CoffeeScript code. Save this code in a file with name 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."
Open the command prompt and compile the .coffee file as shown below.
c:\> coffee -c string_localecompare.coffee
On compiling, it gives you the following 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);
Now, open the command prompt again and run the CoffeeScript file as shown below.
c:\> coffee string_localecompare.coffee
On executing, the CoffeeScript file produces the following output.
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.