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


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


描述

該方法接受beginend index值,並返回調用字符串對象中存在於給定索引值之間的部分。如果我們不傳遞結束索引值,它將字符串的結尾作為結束索引值。

注意�我們還可以使用範圍對字符串進行切片。

用法

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

string.slice( beginslice [, endSlice] )

示例

下麵的例子演示了使用slice()CoffeeScript 代碼中的 JavaScript 方法。將此代碼保存在名為 string_slice.coffee 的文件中

my_string = "Apples are round, and apples are juicy."
result = my_string.slice 3, -2
         
console.log "The required slice of the string is::"+result

打開command prompt並編譯 .coffee 文件,如下所示。

c:\> coffee -c coffee string_slice.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.slice(3, -2);

  console.log("The required slice of the string is::" + result);

}).call(this);

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

c:\> coffee string_slice.coffee 

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

The required slice of the string is::les are round, and apples are juic

相關用法


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