String Object Method In TypeScript: Part 5

String Object Method In TypeScript: Part 5 

 
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 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
  1. 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
  1. IndexOf() {  
  2.   var str1 = prompt("please enter string");  
  3.   var indexstr = prompt("Enter letter or string");  
  4.   var result = str1.concat(indexstr);  
  5.   if (result.length > 0) {  
  6.    var Index = str1.indexOf(indexstr);  
  7.    var span = document.createElement("span");  
  8.    span.style.color = "green";  
  9.    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";  
  10.    document.body.appendChild(span);  
  11.   }  
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
  1. class IndexOf_method  
  2. {  
  3.  IndexOf()  
  4.  {  
  5.   var str1 = prompt("please enter string");  
  6.   var indexstr = prompt("Enter letter or string");  
  7.   var result = str1.concat(indexstr);  
  8.   if (result.length > 0)  
  9.   {  
  10.    var Index = str1.indexOf(indexstr);  
  11.    var span = document.createElement("span");  
  12.    span.style.color = "green";  
  13.    span.innerText = "indexOf Method \n String is-> " + str1 + "\n Enter letter or String is-> " + indexstr +  
  14.     "\n Locate the first occurance of the letter or strings-> " + Index + "\n";  
  15.    document.body.appendChild(span);  
  16.   }  
  17.  }  
  18. }  
  19. window.onload = () =>  
  20.  {  
  21.   var obj = new IndexOf_method();  
  22.   obj.IndexOf();  
  23.  };  
indexOf_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="indexOf.js"></script>  
  9.     </head>  
  10.     <body>  
  11.         <h3>indexOf() String Object Method In TypeScript</h3>  
  12.         <div id="content"/>  
  13.     </body>  
  14. </html>  
indexOf.js
  1. var IndexOf_method = (function() {  
  2.  function IndexOf_method() {}  
  3.  IndexOf_method.prototype.IndexOf = function() {  
  4.   var str1 = prompt("please enter string");  
  5.   var indexstr = prompt("Enter letter or string");  
  6.   var result = str1.concat(indexstr);  
  7.   if (result.length > 0) {  
  8.    var Index = str1.indexOf(indexstr);  
  9.    var span = document.createElement("span");  
  10.    span.style.color = "green";  
  11.    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";  
  12.    document.body.appendChild(span);  
  13.   }  
  14.  };  
  15.  return IndexOf_method;  
  16. })();  
  17. window.onload = function() {  
  18.  var obj = new IndexOf_method();  
  19.  obj.IndexOf();  
  20. };  
Output 1
 
Enter a string and click on "Ok":
 
 enter-string.jpg
 
Output 2
 
If the indexOf value is a string then:
 
 string.jpg
 
Output 3
 
 string-result.jpg
 
Output 4
 
If the indexOf value is a letter then:
 
 letter.jpg
 
Output 5
 
 letter-result.jpg