Cadena CoffeeScript - charAt ()

Descripción

los charAt() El método de JavaScript devuelve el carácter de la cadena actual que existe en el índice especificado.

Los caracteres de una cadena se indexan de izquierda a derecha. El índice del primer carácter es 0 y el índice del último carácter es uno menos que la longitud de la cadena. (stringName_length - 1)

Sintaxis

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

string.charAt(index);

Acepta un valor entero que representa el índice de la Cadena y devuelve el carácter en el índice especificado.

Ejemplo

El siguiente ejemplo demuestra el uso de charAt()método de JavaScript en el código CoffeeScript. Guarde este código en un archivo con nombrestring_charat.coffee

str = "This is string"  

console.log "The character at the index (0) is:" + str.charAt 0   
console.log "The character at the index (1) is:" + str.charAt 1   
console.log "The character at the index (2) is:" + str.charAt 2   
console.log "The character at the index (3) is:" + str.charAt 3   
console.log "The character at the index (4) is:" + str.charAt 4   
console.log "The character at the index (5) is:" + str.charAt 5

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

c:\> coffee -c string_charat.coffee

Al compilar, le da el siguiente JavaScript.

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

  str = "This is string";

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

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

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

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

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

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

}).call(this);

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

c:\> coffee string_charat.coffee

Al ejecutarse, el archivo CoffeeScript produce la siguiente salida.

The character at the index (0) is:T
The character at the index (1) is:h
The character at the index (2) is:i
The character at the index (3) is:s
The character at the index (4) is:
The character at the index (5) is:i