Cadena CoffeeScript - charCodeAt ()

Descripción

Este método devuelve un número que indica el valor Unicode del carácter en el índice dado.

Los puntos de código Unicode van de 0 a 1,114,111. Los primeros 128 puntos de código Unicode son una coincidencia directa de la codificación de caracteres ASCII.charCodeAt() siempre devuelve un valor menor que 65.536.

Sintaxis

A continuación se muestra la sintaxis del método charCodeAt () de JavaScript. Podemos usar el mismo método del código CoffeeScript.

string. charCodeAt(index)

Acepta un valor entero que representa el índice de la Cadena y devuelve el valor Unicode del carácter existente en el índice especificado de la Cadena. VuelveNaN si el índice dado no es entre 0 y 1 menor que la longitud de la cadena.

Ejemplo

El siguiente ejemplo demuestra el uso de charCodeAt()método de JavaScript en el código CoffeeScript. Guarde este código en un archivo con nombrestring_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

Abre el command prompt y compile el archivo .coffee como se muestra a continuación.

c:\> coffee -c string_charcodeat.coffee

Al compilar, le da el siguiente 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);

Ahora, abre el command prompt nuevamente y ejecute el archivo CoffeeScript como se muestra a continuación.

c:\> coffee string_charcodeat.coffee

Al ejecutarse, el archivo CoffeeScript produce la siguiente salida.

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