String Object Method In TypeScript: Part 4

String Object Method In TypeScript: Part 4 

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

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 the 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 return a new string with a specified portion replaced by a new value. The Replace() method does not change the original string.
 
Syntax
  1. 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
  1. Replace() {  
  2.  var str = prompt("Please enter string");  
  3.  var replacestr = prompt("enter replace string");  
  4.  var result = str.replace(str, replacestr);  
  5.  var div = document.getElementById("content");  
  6.  div.style.color = "green";  
  7.  div.innerText = "Replace Method \n  Replace " + str + " with " + replacestr + "\n String is ->" + result + "\n";  
  8. }  
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
  1. class Replace_method {  
  2.  Replace() {  
  3.   var str = prompt("Please enter string");  
  4.   var replacestr = prompt("enter replace string");  
  5.   var result = str.replace(str, replacestr);  
  6.   var div = document.getElementById("content");  
  7.   div.style.color = "green";  
  8.   div.innerText = "Replace Method \n  Replace " + str + " with " + replacestr + "\n String is ->" + result + "\n";  
  9.  }  
  10. }  
  11. window.onload = () => {  
  12.  var obj = new Replace_method();  
  13.  obj.Replace();  
  14. };  
Replace_MethodDemo.htm
  1. <!DOCTYPE html>  
  2. <html lang="en"  
  3.     xmlns="http://www.w3.org/1999/xhtml">  
  4.     <head>  
  5.         <meta charset="utf-8" />  
  6.         <title>TypeScript HTML App</title>  
  7.         <link rel="stylesheet" href="app.css" type="text/css" />  
  8.         <script src="Replace.js"></script>  
  9.     </head>  
  10.     <body>  
  11.         <h3>Replace() String Object Method In TypeScript</h3>  
  12.         <div id="content"/>  
  13.     </body>  
  14. </html>  
Replace.js
  1. var Replace_method = (function() {  
  2.  function Replace_method() {}  
  3.  Replace_method.prototype.Replace = function() {  
  4.   var str = prompt("Please enter string");  
  5.   var replacestr = prompt("enter replace string");  
  6.   var result = str.replace(str, replacestr);  
  7.   var div = document.getElementById("content");  
  8.   div.style.color = "green";  
  9.   div.innerText = "Replace Method \n  Replace " + str + " with " + replacestr + "\n String is ->" + result + "\n";  
  10.  };  
  11.  return Replace_method;  
  12. })();  
  13. window.onload = function() {  
  14.  var obj = new Replace_method();  
  15.  obj.Replace();  
  16. };  
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


Similar Articles