Extracting String Characters in javascript

Something Like that:
charAt() charCodeAt()

Extracting String Characters
There are 2 safe methods for extracting string characters:
charAt(position)
charCodeAt(position)

charAt()

The charAt() method returns the character at a specified index (position) in a string:

For that you need to create first html code.

HTML

<p id="demo"></p>

After that add this JS code.

JS

var str = "HELLO Home2it";
res = str.charAt(0);
document.getElementById("demo").innerHTML = res;

[codepen_embed height=”265″ theme_id=”0″ slug_hash=”gxpddL” default_tab=”result” user=”pradeepanvi”]See the Pen charAt in javascript by Pradeep Kumar (@pradeepanvi) on CodePen.[/codepen_embed]

charCodeAt()

The charCodeAt() method returns the unicode of the character at a specified index in a string

For that you need to create first html code.

HTML

<p id="demo"></p>

After that add this JS code.

JS

var str = "HELLO Home2it";
res = str.charCodeAt(0);
document.getElementById("demo").innerHTML = res;

[codepen_embed height=”265″ theme_id=”0″ slug_hash=”jLPvQZ” default_tab=”result” user=”pradeepanvi”]See the Pen charCodeAt in javascript by Pradeep Kumar (@pradeepanvi) on CodePen.[/codepen_embed]

Now you can see 72.

What is 72
This is character code on keyboard use alt+72 and then you can see H

Leave a Reply

Your email address will not be published. Required fields are marked *