String Object Method In TypeScript: Part 1

Introduction

The String object is used to manage a series of characters. The String object contains complete information about strings. In TypeScript, String objects are created with "new String()".

In this article I am describing the TypeScript charAt and charCodeAt string methods.

chatAt() Method

In TypeScript, the charAt() method returns the character at the specified position in the string. Characters in a string proceed (are indexed) from left to right. The position of the first character is 0 and the position of the last character in a string is string.length - 1.

Syntax

string.charAt(position)

position: is a required parameter; represents the position of the character.

Function

A sample use of charAt is:

charAt(str:string)

    {

        var position = str.charAt(0);

        var span = document.createElement("span");

        span.innerText = "charAt Method \n Display the first character of a string-> " + position + "\n";

        document.body.appendChild(span);

    }

 

charCodeAt() Method

 

In TypeScript the charCodeAt() method is returns a number that represents the Unicode value of the character at the given position.

Syntax

string.charCodeAt(position)

position: is a required parameter; the position value is between 0 and the position of the last character, which is 1 less than the length of the string. The default position is 0.

Function

A sample use of charCodeAt is:

charCodeAt(str:string)

    {

        var unicode = str.charCodeAt(str.length-1);

        var span = document.createElement("span");

        span.style.color = "green";

        span.innerText = "charCodeAt Method \n Display the unicode of the last character in a string-> " + unicode + "\n";

        document.body.appendChild(span);

    }

 

The following example shows how to use charAt and charCodeAt methods in TypeScript. In this example, we get a string from the user. The charAt() method returns the character at the specified position and the charCodeAt() method returns a Unicode character at a specified index.

 

Complete Program

chatAt_charCodeAt.ts

class charAt_charCodeAt

{

    charAt(str:string)

    {

        var position = str.charAt(0);

        var span = document.createElement("span");

        span.innerText = "charAt Method \n Display the first character of a string-> " + position + "\n";

        document.body.appendChild(span);

    }

    charCodeAt(str:string)

    {

        var unicode = str.charCodeAt(str.length-1);

        var span = document.createElement("span");

        span.style.color = "green";

        span.innerText = "charCodeAt Method \n Display the unicode of the last character in a string-> " + unicode + "\n";

        document.body.appendChild(span);

    }

}

window.onload = () =>

{

    var obj = new charAt_charCodeAt();

    var str = prompt("Please enter a string");

    obj.charAt(str);

    obj.charCodeAt(str);

};

 

chatAt_charCodeAt_MethodDemo.htm

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">

<head>

    <meta charset="utf-8" />

    <title>TypeScript HTML App</title>

    <link rel="stylesheet" href="app.css" type="text/css" />

    <script src="charAt_charCodeAt.js"></script>

</head>

<body>

    <h3>charAt() and charCodeAt() String Object Method In TypeScript</h3>

    <div id="content"/>

</body>

</html>

 

chatAt_charCodeAt.js

var charAt_charCodeAt = (function () {

    function charAt_charCodeAt() { }

    charAt_charCodeAt.prototype.charAt = function (str) {

        var position = str.charAt(0);

        var span = document.createElement("span");

        span.innerText = "charAt Method \n Display the first character of a string-> " + position + "\n";

        document.body.appendChild(span);

    };

    charAt_charCodeAt.prototype.charCodeAt = function (str) {

        var unicode = str.charCodeAt(str.length - 1);

        var span = document.createElement("span");

        span.style.color = "green";

        span.innerText = "charCodeAt Method \n Display the unicode of the last character in a string-> " + unicode + "\n";

        document.body.appendChild(span);

    };

    return charAt_charCodeAt;

})();

window.onload = function () {

    var obj = new charAt_charCodeAt();

    var str = prompt("Please enter a string");

    obj.charAt(str);

    obj.charCodeAt(str);

};

//@ sourceMappingURL=charAt_charCodeAt.js.map 


 

Output 1

Enter a string and click on "Ok":


enter-string.jpg


Output 2

 

result.jpg

 

Up Next
    Ebook Download
    View all

    Test

    Read by 16 people
    Download Now!
    Learn
    View all