
- CoffeeScript 教程
- CoffeeScript - 主頁
- CoffeeScript - 概述
- CoffeeScript - 環境
- CoffeeScript - 命令列實用程式
- CoffeeScript - 語法
- CoffeeScript - 資料型別
- CoffeeScript - 變數
- CoffeeScript - 運算子和別名
- CoffeeScript - 條件語句
- CoffeeScript - 迴圈
- CoffeeScript - 理解
- CoffeeScript - 函式
- CoffeeScript 面向物件
- CoffeeScript - 字串
- CoffeeScript - 陣列
- CoffeeScript - 物件
- CoffeeScript - 範圍
- CoffeeScript - 分散
- CoffeeScript - 日期
- CoffeeScript - 數學
- CoffeeScript - 異常處理
- CoffeeScript - 正則表示式
- CoffeeScript - 類和繼承
- CoffeeScript 高階
- CoffeeScript - Ajax
- CoffeeScript - jQuery
- CoffeeScript - MongoDB
- CoffeeScript - SQLite
- CoffeeScript 有用資源
- CoffeeScript - 快速指南
- CoffeeScript - 有用資源
- CoffeeScript - 討論
CoffeeScript 字串 - localeCompare()
描述
此方法接受一個字串,並將其與呼叫字串物件進行比較。如果兩者相等,它返回 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.
coffeescript_strings.htm
廣告