當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


CoffeeScript String split()用法及代碼示例


描述

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

用法

下麵給出的是split()JavaScript 的方法。我們可以在 CoffeeScript 代碼中使用相同的方法。

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

示例

下麵的例子演示了使用split()CoffeeScript 代碼中的 JavaScript 方法。將此代碼保存在具有名稱的文件中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

打開command prompt並編譯 .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);

現在,打開command prompt再次運行 CoffeeScript 文件,如下所示。

c:\> coffee string_split.coffee 

在執行時,CoffeeScript 文件產生以下輸出。

The two resultant strings of the split operation are::
Apples are round, and apples are juicy.

相關用法


注:本文由純淨天空篩選整理自 CoffeeScript String - split()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。