JavaScript > String Methods

String Charat in JavaScript

How to return a particular character of a string in JavaScript?


Charat

Charat is an string method {charAt() method} in JavaScript, which is used to return the character from the specified index. The charAt() method have one require parameter i.e 'index'.

If we want to retun the character from the start side just we need to give the index number to the charAt() method {String.charAt(2)}, the index counting of characters in a string starts from '0', that means first character is '0', second character is '1' like that and so on....., in case if we want to return the character end side we need to use the index 'String.length-1' for the first character, 'String.length-2' for the second character,and so on.....from the last.

In JavaScript, by using JavaScript charAt() method we can return the particular character of a string. For example if we want to return the 8th character of the string we can use the below code.

Returning the characters from the start. 

   <p>Click on the "Button" for returning the 8th character of a string "SN ITFunda Service LLP".</p>
   <input type="button" value="Button" onclick="Function()"/>
   <p id="myId"></p>

   <script>
       function Function() {
           var a = "SN ITFunda Service LLP";
           var res = a.charAt(8)
           document.getElementById("myId").innerHTML = res;
       }
    </script>

In the above code snippet we have given ID as "myId" to the second <p> element and we used the string "SN ITFunda Service LLP" in the script code, and along with that we have given value 8 to the charAt() method, that returns the 8th character at the specified index in a string.

There is a button with value "Button" in the HTML page onclick of the button fires the function() in the script code and gives the result.

OUTPUT

Form the above output we can notice that the 8th character of the string "SN ITFunda Service LLP" is "d". While counting the characters of the string the charAt() method starts counting from '0' to the nth number of the string.

Returning the first character of a string.

<p>Click on the "Button" for returning the 1st character from the start of a string "SN ITFunda Service LLP".</p>
<input type="button" value="Button" onclick="Function()"/>
<p id="myId"></p>

<script>
    function Function() {
        var a = "SN ITFunda Service LLP";
        var res = a.charAt(0);
        document.getElementById("myId").innerHTML = res;
     }
</script>

Retuning first character

We need to return the first character of a string, for that we are using charAt(0) method. Onclick of the button returns the first character of a string as output.

OUTPUT

Returning the last character of a string.

<p>Click on the "Button" for returning the last character of a string "SN ITFunda Service LLP".</p>
<input type="button" value="Button" onclick="Function()"/>
<p id="myId"></p>

<script>
    function Function() {
        var a = "SN ITFunda Service LLP";
        var res = a.charAt(a.length-1);
        document.getElementById("myId").innerHTML = res;
     }
</script>

Returning last character

We need to return the last character of a string, for that we are using charAt(a.length-1) method. Onclick of the button returns the last character of a string as output.

OUTPUT

Returning the characters from start & end of a string.

<p>Click on the "Button" for returning the characters of a string "SN ITFunda Service LLP" from Start & End.</p>
<input type="button" value="Button" onclick="Function()"/>
<p id="myId"></p>

<script>
    function Function() {
        var a = "SN ITFunda Service LLP";

        var res = "2nd character from Start:" + a.charAt(1) + "</br>"
        + "2nd character from End:" + a.charAt(a.length - 2) + "</br>"
        + "3rd character from Start:" + a.charAt(2) + "</br>"
        + "3rd character from End:" + a.charAt(a.length - 3);
        document.getElementById("myId").innerHTML = res;
     }
</script>

Returning characters from Start & End

We are returning the diffirent characters of a string from Start & End positions by using charAt() method.

OUTPUT

In the above output we can notice that 3rd character from Start is showing empty, because the string value "SN ITFunda Service LLP" consists of space in the place of 3rd character, so it returning the empty space in the output. Space, comma, full stop, etc....are consider as a characters in a string.

 Views: 1241 | Post Order: 67



Write for us