Coffeescript-string-charcodeat

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

CoffeeScript文字列-charCodeAt()

説明

このメソッドは、指定されたインデックスにある文字のUnicode値を示す数値を返します。

Unicodeコードポイントの範囲は0〜1,114,111です。 最初の128個のUnicodeコードポイントは、ASCII文字エンコードの直接一致です。 * charCodeAt()*は常に65,536未満の値を返します。

構文

以下は、JavaScriptのcharCodeAt()メソッドの構文です。 CoffeeScriptコードから同じメソッドを使用できます。

string. charCodeAt(index)

Stringのインデックスを表す整数値を受け入れ、Stringの指定されたインデックスに存在する文字のUnicode値を返します。 指定されたインデックスが文字列の長さよりも0〜1の間でない場合は、 NaN を返します。

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

str = "This is string"

console.log "The Unicode of the character at the index (0) is:" + str.charCodeAt 0
console.log "The Unicode of the character at the index (1) is:" + str.charCodeAt 1
console.log "The Unicode of the character at the index (2) is:" + str.charCodeAt 2
console.log "The Unicode of the character at the index (3) is:" + str.charCodeAt 3
console.log "The Unicode of the character at the index (4) is:" + str.charCodeAt 4
console.log "The Unicode of the character at the index (5) is:" + str.charCodeAt 5
  • コマンドプロンプト*を開き、以下に示すように.coffeeファイルをコンパイルします。
c:\> coffee -c string_charcodeat.coffee

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

//Generated by CoffeeScript 1.10.0
(function() {
  var str;

  str = "This is string";

  console.log("The Unicode of the character at the index (0) is:" + str.charCodeAt(0));

  console.log("The Unicode of the character at the index (1) is:" + str.charCodeAt(1));

  console.log("The Unicode of the character at the index (2) is:" + str.charCodeAt(2));

  console.log("The Unicode of the character at the index (3) is:" + str.charCodeAt(3));

  console.log("The Unicode of the character at the index (4) is:" + str.charCodeAt(4));

  console.log("The Unicode of the character at the index (5) is:" + str.charCodeAt(5));

}).call(this);

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

c:\> coffee string_charcodeat.coffee

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

The Unicode of the character at the index (0) is:84
The Unicode of the character at the index (1) is:104
The Unicode of the character at the index (2) is:105
The Unicode of the character at the index (3) is:115
The Unicode of the character at the index (4) is:32
The Unicode of the character at the index (5) is:105