String Object Method In TypeScript: Part 4

Before reading this article, please go through the following articles:

  1. String Object Method In TypeScript: Part 1

  2. String Object Method In TypeScript: Part 2

  3. String Object Method In TypeScript: Part 3

 

Introduction

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

In this article I am describing string object's "replace" method in TypeScript.

Replace() Method

 

In TypeScript, the "replace()" method is used to search a string for a specified value, and it will be return a new string with a specified portion replaced by a new value. The Replace() method does not change the original string.

Syntax

string.replace(searchvalue,newvalue)

searchvalue is a required parameter. It is the specified value to be replaced by the new value.

newvalue is a required parameter. This value replaces the searchvalue.

Function

Replace()

    {

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

        var replacestr = prompt("enter replace string");

        var result = str.replace(str,replacestr);

        var div = document.getElementById("content");

        div.style.color = "green";

        div.innerText = "Replace Method \n  Replace "+str+" with "+replacestr+"\n String is ->"+result+ "\n";

    }

 

The following example shows how to use the replace method in TypeScript. In this example, we get the string and its replacement string from the user. The replace method replaces the specified value by the new value.

 

Complete Program

Replace.ts

class Replace_method

{

    Replace()

    {

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

        var replacestr = prompt("enter replace string");

        var result = str.replace(str,replacestr);

        var div = document.getElementById("content");

        div.style.color = "green";

        div.innerText = "Replace Method \n  Replace "+str+" with "+replacestr+"\n String is ->"+result+ "\n";

    }

}

window.onload = () =>

{

    var obj = new Replace_method();

    obj.Replace();

};

Replace_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="Replace.js"></script>

</head>

<body>

    <h3>Replace() String Object Method In TypeScript</h3>

    <div id="content"/>

</body>

</html>

Replace.js

var Replace_method = (function () {

    function Replace_method() { }

    Replace_method.prototype.Replace = function () {

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

        var replacestr = prompt("enter replace string");

        var result = str.replace(str, replacestr);

        var div = document.getElementById("content");

        div.style.color = "green";

        div.innerText = "Replace Method \n  Replace " + str + " with " + replacestr + "\n String is ->" + result + "\n";

    };

    return Replace_method;

})();

window.onload = function () {

    var obj = new Replace_method();

    obj.Replace();

};

//@ sourceMappingURL=Replace.js.map


 

Output 1

Enter a string and click on "Ok":

 enter-first-string.jpg

Output 2

Enter the replacement string:

replace-string.jpg


Output 3

 result.jpg

 

Up Next
    Ebook Download
    View all

    Test

    Read by 16 people
    Download Now!
    Learn
    View all