Coffeescript-string-indexof
提供:Dev Guides
CoffeeScript文字列-indexOf()
説明
このメソッドはサブ文字列を受け入れ、呼び出し元のStringオブジェクト内での*最初の*発生のインデックスを返します。 また、検索の開始点となるオプションパラメータ fromIndex も受け入れます。 値が見つからない場合、このメソッドは-1を返します。
構文
以下に、JavaScriptの* indexOf()*メソッドの構文を示します。 CoffeeScriptコードから同じメソッドを使用できます。
string.indexOf(searchValue[, fromIndex])
例
次の例は、CoffeeScriptコードでJavaScriptの* indexOf()メソッドを使用する方法を示しています。 このコードを *string_indexof.coffee という名前のファイルに保存します
str1 = "This is string one"
index = str1.indexOf "string"
console.log "indexOf the given string string is :" + index
index = str1.indexOf "one"
console.log "indexOf the given string one is :" + index
- コマンドプロンプト*を開き、以下に示すように.coffeeファイルをコンパイルします。
c:\> coffee -c string_indexof.coffee
コンパイル時に、次のJavaScriptが提供されます。
//Generated by CoffeeScript 1.10.0
(function() {
var index, str1;
str1 = "This is string one";
index = str1.indexOf("string");
console.log("indexOf the given string string is :" + index);
index = str1.indexOf("one");
console.log("indexOf the given string one is :" + index);
}).call(this);
次に、*コマンドプロンプト*を再度開き、以下に示すようにCoffeeScriptファイルを実行します。
c:\> coffee string_indexof.coffee
CoffeeScriptファイルを実行すると、次の出力が生成されます。
indexOf the given string string is :8
indexOf the given string one is :15