CoffeeScript 字串 - split()



描述

此方法用於將一個字串分成小部分。它接受一個特殊字元和一個整數。字元充當分隔符並指示在何處分割字串,整數指示字串被分成多少部分。如果我們不傳遞分隔符,則會返回整個字串。

語法

下面給出了 JavaScript 的 split() 方法的語法。我們可以在 CoffeeScript 程式碼中使用相同的方法。

string.split([separator][, limit])

示例

以下示例演示如何在 CoffeeScript 程式碼中使用 JavaScript 的 split() 方法。將此程式碼儲存在名稱為 string_split.coffee 的檔案中

my_string = "Apples are round, and apples are juicy."
result = my_string.split " ", 3
         
console.log "The two resultant strings of the split operation are :: "
console.log my_string

開啟 命令提示符 並編譯 .coffee 檔案,如下所示。

c:\> coffee -c coffee string_split.coffee

在編譯時,它會給你以下 JavaScript。

// Generated by CoffeeScript 1.10.0
(function() {
  var my_string, result;

  my_string = "Apples are round, and apples are juicy.";

  result = my_string.split(" ", 3);

  console.log("The two resultant strings of the split operation are :: ");

  console.log(my_string);

}).call(this);

現在,再次開啟 命令提示符 並執行 CoffeeScript 檔案,如下所示。

c:\> coffee string_split.coffee 

在執行時,CoffeeScript 檔案產生以下輸出。

The two resultant strings of the split operation are ::
Apples are round, and apples are juicy.
coffeescript_strings.htm
廣告
© . All rights reserved.