slice()

优质
小牛编辑
128浏览
2023-12-01

描述 (Description)

此方法接受beginend index值,并返回给定索引值之间存在的调用字符串对象的部分。 如果我们没有传递结束索引值,则将字符串的结尾作为结束索引值。

Note - 我们也可以使用范围切片字符串。

语法 (Syntax)

下面给出了JavaScript的slice()方法的语法。 我们可以在CoffeeScript代码中使用相同的方法。

string.slice( beginslice [, endSlice] )

例子 (Example)

以下示例演示了CoffeeScript代码中JavaScript的slice()方法的用法。 将此代码保存在名为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