Before reading this article, please go through the following articles:
-
String Object Method In TypeScript: Part 1
-
String Object Method In TypeScript: Part 2
-
String Object Method In TypeScript: Part 3
-
String Object Method In Typescript: Part 4
Introduction
The String object is used to manage a series of characters. The String object contains all the information about a string. In TypeScript, String objects are created with "new String()".
In this article I am describing the string object's "indexOf" method in TypeScript.
indexOf() Method
In TypeScript, the indexOf() method searches a string for the first occurrence of the search string starting at the position specified, or at the beginning if the position is not specified. If the search string is found, it will return the position (starting from 0) in the string. If the search string is not found, it will return -1.
Syntax
string.indexOf(searchvalue,position) |
searchvalue It is the required parameter. It represents the search value.
position is an optional parameter. The position to start the search. The default value is 0. It can be any integer value between 0 and one less than the length of the string.
Function
IndexOf()
{
var str1 = prompt("please enter string");
var indexstr = prompt("Enter letter or string");
var result = str1.concat(indexstr);
if (result.length > 0)
{
var Index = str1.indexOf(indexstr);
var span = document.createElement("span");
span.style.color = "green";
span.innerText = "indexOf Method \n String is-> " + str1 + "\n Enter letter or String is-> " + indexstr +
"\n Locate the first occurance of the letter or strings-> " + Index + "\n";
document.body.appendChild(span);
}
The following example shows how to use the indexOf method in TypeScript. If you omit the position parameter, then the search is performed from the start of the string. If the search string is not found then the method will return -1.
Complete Program
indexOf.ts
class IndexOf_method
{
IndexOf()
{
var str1 = prompt("please enter string");
var indexstr = prompt("Enter letter or string");
var result = str1.concat(indexstr);
if (result.length > 0)
{
var Index = str1.indexOf(indexstr);
var span = document.createElement("span");
span.style.color = "green";
span.innerText = "indexOf Method \n String is-> " + str1 + "\n Enter letter or String is-> " + indexstr +
"\n Locate the first occurance of the letter or strings-> " + Index + "\n";
document.body.appendChild(span);
}
}
}
window.onload = () =>
{
var obj = new IndexOf_method();
obj.IndexOf();
};
indexOf_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="indexOf.js"></script>
</head>
<body>
<h3>indexOf() String Object Method In TypeScript</h3>
<div id="content"/>
</body>
</html>
indexOf.js
var IndexOf_method = (function () {
function IndexOf_method() { }
IndexOf_method.prototype.IndexOf = function () {
var str1 = prompt("please enter string");
var indexstr = prompt("Enter letter or string");
var result = str1.concat(indexstr);
if(result.length > 0) {
var Index = str1.indexOf(indexstr);
var span = document.createElement("span");
span.style.color = "green";
span.innerText = "indexOf Method \n String is-> " + str1 + "\n Enter letter or String is-> " + indexstr +
"\n Locate the first occurance of the letter or strings-> " + Index + "\n";
document.body.appendChild(span);
}
};
return IndexOf_method;
})();
window.onload = function () {
var obj = new IndexOf_method();
obj.IndexOf();
};
//@ sourceMappingURL=indexOf.js.map
Output 1
Enter a string and click on "Ok":
Output 2
If the indexOf value is a string then:
Output 3
Output 4
If the indexOf value is a letter then:
Output 5