Before reading this article, please go through the following articles:
-
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 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
string.concat(string1,string2, ...,string..n) |
Function
concat()
{
var str1 = prompt("please enter string");
var concatstr = prompt("Enter concatenation string");
var result = str1.concat(concatstr);
var span = document.createElement("span");
span.style.color = "green";
span.innerText = "concat Method \n First String-> "+str1+"\n Second String-> "+concatstr+
"\nDisplay string which join two diffrent strings-> " + result + "\n";
document.body.appendChild(span);
}
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
class concat_method
{
concat()
{
var str1 = prompt("please enter string");
var concatstr = prompt("Enter concatenation string");
var result = str1.concat(concatstr);
var span = document.createElement("span");
span.style.color = "green";
span.innerText = "concat Method \n First String-> "+str1+"\n Second String-> "+concatstr+
"\nDisplay string which join two diffrent strings-> " + result + "\n";
document.body.appendChild(span);
}
}
window.onload = () =>
{
var obj = new concat_method();
obj.concat();
};
concat_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="concat_method.js"></script>
</head>
<body>
<h3>concat() String Object Method In TypeScript</h3>
<div id="content"/>
</body>
</html>
concat_method.js
var concat_method = (function () {
function concat_method() { }
concat_method.prototype.concat = function () {
var str1 = prompt("please enter string");
var concatstr = prompt("Enter concatenation string");
var result = str1.concat(concatstr);
var span = document.createElement("span");
span.style.color = "green";
span.innerText = "concat Method \n First String-> " + str1 + "\n Second String-> " + concatstr +
"\nDisplay string which join two diffrent strings-> " + result + "\n";
document.body.appendChild(span);
};
return concat_method;
})();
window.onload = function () {
var obj = new concat_method();
obj.concat();
};
//@ sourceMappingURL=concat_method.js.map
Output 1
Enter the first string and click on "Ok".
Output 2
Enter the string to be concatenated then click "Ok".
Output 3