Before reading this article, please go through the following articles:
-
concat() and lastIndexOf() array object method in typescript
Introduction
In TypeScript, the array object makes work easier. The TypeScript Array object stores multiple values in a single variable at a time. TypeScript provides many methods.
In this article I am describing the TypeScript array methods Join and lastIndexOf.
Join() Method
In TypeScript the join() method is used to join the elements of an array into a string and return the string. The join() method takes a separator as a parameter that specifies the special character like space, comma and plus or any other character to separate each and every element of the array. When no parameter is passed, this method converts all the elements of the array to strings and concatenates them separated by commas.
Syntax
Function
Join()
{
var fstarry: string[] = ['C','Sharp','Corner', '.com'];
var result = fstarry.join();
var result1 = fstarry.join('+');
var result2 = fstarry.join('*');
var span = document.createElement("span");
span.innerText = "Join Method \n First array Sample join as simple -> " + result + "\n" + "Second array sample join with plus (+) -> " + result1 + "\n" +
"Third array sample join with (*) -> " + result2 +"\n";
document.body.appendChild(span);
}
LastIndexOf() Method
In TypeScript the lastIndexOf() method searches for the last occurance of a string value from the given characters. In the lastIndexOf() method the searchvalue parameter will start from a specified position.
If the position is not set then the search will start from the beginning. The beginning position starts from 0. After searching it will return the index number of values. If it does not find a match then it will return -1. The lastIndexOf() method is case sensitive.
Syntax
array.lastIndexOf(searchvalue,start) |
Function
LastIndexOf()
{
var arrayName: string[] = ['C','Sharp','Corner','VB','Net','Heaven'];
var index = arrayName.lastIndexOf('Corner');
var span = document.createElement("span");
span.style.color = "#7a1111";
span.innerText = "LastIndexOf Method \n LastIndexOf of (Corner) in array is -> " + index;
document.body.appendChild(span);
}
Complete Program
Join_LastIndexOf.ts
class Join_LastIndexOf
{
Join()
{
var fstarry: string[] = ['C','Sharp','Corner', '.com'];
var result = fstarry.join();
var result1 = fstarry.join('+');
var result2 = fstarry.join('*');
var span = document.createElement("span");
span.innerText = "Join Method \n First array Sample join as simple -> " + result + "\n" + "Second array sample join with plus (+) -> " + result1 + "\n" + "Third array sample join with (*) -> " + result2 +"\n";
document.body.appendChild(span);
}
LastIndexOf()
{
var arrayName: string[] = ['C','Sharp','Corner','VB','Net','Heaven'];
var index = arrayName.lastIndexOf('Corner');
var span = document.createElement("span");
span.style.color = "#7a1111";
span.innerText = "LastIndexOf Method \n LastIndexOf of (Corner) in array is -> " + index;
document.body.appendChild(span);
}
}
window.onload = () =>
{
var obj = new Join_LastIndexOf();
obj.Join();
obj.LastIndexOf();
};
Join_LastIndexOf_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="Join_LastIndexOf.js"></script>
</head>
<body>
<h3>Join() and LastIndexOf() Array Method In TypeScript</h3>
</body>
</html>
Join_LastIndexOf.js
var Join_LastIndexOf = (function () {
function Join_LastIndexOf() { }
Join_LastIndexOf.prototype.Join = function () {
var fstarry = [
'C',
'Sharp',
'Corner',
'.com'
];
var result = fstarry.join();
var result1 = fstarry.join('+');
var result2 = fstarry.join('*');
var span = document.createElement("span");
span.innerText = "Join Method \n First array Sample join as simple -> " + result + "\n" + "Second array sample join with plus (+) -> " + result1 + "\n" +
"Third array sample join with (*) -> " + result2 + "\n";
document.body.appendChild(span);
};
Join_LastIndexOf.prototype.LastIndexOf = function () {
var arrayName = [
'C',
'Sharp',
'Corner',
'VB',
'Net',
'Heaven'
];
var index = arrayName.lastIndexOf('Corner');
var span = document.createElement("span");
span.style.color = "#7a1111";
span.innerText = "LastIndexOf Method \n LastIndexOf of (Dot) in array is -> " + index;
document.body.appendChild(span);
};
return Join_LastIndexOf;
})();
window.onload = function () {
var obj = new Join_LastIndexOf();
obj.Join();
obj.LastIndexOf();
};
//@ sourceMappingURL=Join_LastIndexOf.js.map
Output