CoffeeScript String - charCodeAt ()

Descrição

Este método retorna um número que indica o valor Unicode do caractere no índice fornecido.

Os pontos de código Unicode variam de 0 a 1.114.111. Os primeiros 128 pontos de código Unicode são uma correspondência direta da codificação de caracteres ASCII.charCodeAt() sempre retorna um valor menor que 65.536.

Sintaxe

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

string. charCodeAt(index)

Ele aceita um valor inteiro que representa o índice da String e retorna o valor Unicode do caractere existente no índice especificado da String. RetornaNaN se o índice fornecido não for entre 0 e 1 menor que o comprimento da string.

Exemplo

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

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

c:\> coffee -c string_charcodeat.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 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);

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

c:\> coffee string_charcodeat.coffee

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

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