Coffeescript-string-lastindexof

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

CoffeeScript文字列-lastIndexOf()

説明

このメソッドはサブ文字列を受け入れ、呼び出し元のStringオブジェクト内での last オカレンスのインデックスを返します。 また、検索を開始するオプションのパラメーター fromIndex を受け入れ、値が見つからない場合は-1を返します。

構文

JavaScriptの* lastIndexOf()*メソッドの構文は次のとおりです。 CoffeeScriptコードから同じメソッドを使用できます。

string.lastIndexOf(searchValue[, fromIndex])

次の例は、CoffeeScriptコードでJavaScriptの* lastIndexOf()メソッドを使用する方法を示しています。 このコードを *string_lastindexof.coffee という名前のファイルに保存します

str1 = "A sentence does not end with because because, because is a conjunction."
index = str1.lastIndexOf  "because"
console.log "lastIndexOf the given string because is :" + index

index = str1.lastIndexOf "a"
console.log "lastIndexOf the letter a is :"+ index
  • コマンドプロンプト*を開き、以下に示すように.coffeeファイルをコンパイルします。
c:\> coffee -c string_last_indexof.coffee

コンパイル時に、次のJavaScriptが提供されます。

//Generated by CoffeeScript 1.10.0
(function() {
  var index, str1;

  str1 = "A sentence does not end with because, because because is a conjunction.";

  index = str1.lastIndexOf("because");

  console.log("lastIndexOf the given string because is :" + index);

  index = str1.lastIndexOf("a");

  console.log("lastIndexOf the letter a is :" + index);

}).call(this);

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

c:\> coffee string_last_indexof.coffee

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

lastIndexOf the given string because is :46
lastIndexOf the letter a is :57