String Object Method In TypeScript: Part 2

String Object Method In TypeScript: Part 2 

 
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 complete information about the string. In TypeScript, the String objects are created with new String().
 
In this article, I am describing the TypeScript "concat" string method.
 

concat() Method

 
In TypeScript the concat() method is used to concatenate two or more strings. It returns a new string that is the concatenation of this string with the other strings specified in the parameter list. The concat method is the same operation performed by the + operator when one of the two operands is a string.
 
Syntax
  1. string.concat(string1,string2, ...,string..n)   
Function
  1. concat() {  
  2.  var str1 = prompt("please enter string");  
  3.  var concatstr = prompt("Enter concatenation string");  
  4.  var result = str1.concat(concatstr);  
  5.  var span = document.createElement("span");  
  6.  span.style.color = "green";  
  7.  span.innerText = "concat Method \n First String-> " + str1 + "\n Second String-> " + concatstr + "\nDisplay string which join two diffrent strings-> " + result + "\n";  
  8.  document.body.appendChild(span);  
  9. }  
The following example shows how to use the concat method in TypeScript. In this example, we get two strings from the user. Then we concatenate the strings using the concat method.
 

Complete Program

 
concat_method.ts
  1. class concat_method  
  2. {  
  3.  concat()  
  4.  {  
  5.   var str1 = prompt("please enter string");  
  6.   var concatstr = prompt("Enter concatenation string");  
  7.   var result = str1.concat(concatstr);  
  8.   var span = document.createElement("span");  
  9.   span.style.color = "green";  
  10.   span.innerText = "concat Method \n First String-> " + str1 + "\n Second String-> " + concatstr +  
  11.    "\nDisplay string which join two diffrent strings-> " + result + "\n";  
  12.   document.body.appendChild(span);  
  13.  }  
  14. }  
  15. window.onload = () =>  
  16.  {  
  17.   var obj = new concat_method();  
  18.   obj.concat();  
  19.  };  
concat_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="concat_method.js"></script>  
  9.     </head>  
  10.     <body>  
  11.         <h3>concat() String Object Method In TypeScript</h3>  
  12.         <div id="content"/>  
  13.     </body>  
  14. </html>  
concat_method.js
  1. var concat_method = (function() {  
  2.  function concat_method() {}  
  3.  concat_method.prototype.concat = function() {  
  4.   var str1 = prompt("please enter string");  
  5.   var concatstr = prompt("Enter concatenation string");  
  6.   var result = str1.concat(concatstr);  
  7.   var span = document.createElement("span");  
  8.   span.style.color = "green";  
  9.   span.innerText = "concat Method \n First String-> " + str1 + "\n Second String-> " + concatstr + "\nDisplay string which join two diffrent strings-> " + result + "\n";  
  10.   document.body.appendChild(span);  
  11.  };  
  12.  return concat_method;  
  13. })();  
  14. window.onload = function() {  
  15.  var obj = new concat_method();  
  16.  obj.concat();  
  17. };  
Output 1
 
Enter the first string and click on "Ok".
 
 first-string.jpg
 
Output 2
 
Enter the string to be concatenated then click "Ok".
 
second-string.jpg
 
Output 3
 
 result.jpg


Similar Articles