Coffeescript-string-slice

提供:Dev Guides
移動先:案内検索

CoffeeScript文字列-slice()

説明

このメソッドは、 begin および end index 値を受け入れ、指定されたインデックス値の間に存在する呼び出し文字列オブジェクトの部分を返します。 終了インデックス値を渡さない場合、終了インデックス値として文字列の終わりを取ります。

-範囲を使用して文字列をスライスすることもできます。

構文

以下に、JavaScriptの* slice()*メソッドの構文を示します。 CoffeeScriptコードで同じメソッドを使用できます。

string.slice( beginslice [, endSlice] )

次の例は、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
  • コマンドプロンプト*を開き、以下に示すように.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);

次に、*コマンドプロンプト*を再度開き、以下に示すようにCoffeeScriptファイルを実行します。

c:\> coffee string_slice.coffee

CoffeeScriptファイルを実行すると、次の出力が生成されます。

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