CoffeeScript String - charAt ()

Descrição

o charAt() método de JavaScript retorna o caractere da string atual que existe no índice especificado.

Os caracteres em uma string são indexados da esquerda para a direita. O índice do primeiro caractere é 0 e o índice do último caractere é um a menos que o comprimento da string. (stringName_length - 1)

Sintaxe

A seguir está a sintaxe do método charAt () do JavaScript. Podemos usar o mesmo método do código CoffeeScript.

string.charAt(index);

Ele aceita um valor inteiro que representa o índice da String e retorna o caractere no índice especificado.

Exemplo

O exemplo a seguir demonstra o uso de charAt()método de JavaScript no código CoffeeScript. Salve este código em um arquivo com o nomestring_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

Abra o command prompt e compilar o arquivo .coffee conforme mostrado abaixo.

c:\> coffee -c string_charat.coffee

Na compilação, ele fornece o seguinte 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);

Agora, abra o command prompt novamente e execute o arquivo CoffeeScript conforme mostrado abaixo.

c:\> coffee string_charat.coffee

Ao ser executado, o arquivo CoffeeScript produz a seguinte saída.

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