String Object Method In TypeScript: Part 3

In this article you will learn about the string object's Search method in TypeScript.

String Object Method In TypeScript: Part 3 

 
Before reading this article, please go through the following articles:

Introduction

 
The String object is used to manage a series of characters. A String object contains complete information about a string. In TypeScript, String objects are created with "new String()".
 
In this article, I am describing the string object's Search method in TypeScript.
 

Search() Method

 
In TypeScript, the search() method is used to search a string for a specified value and it will return the position of the match. If no match is found then it will return -1.
 
Syntax
  1. string.search(searchvalue)  
searchvalue: a required parameter; it the value to search for in a string.
 
Function
  1. Search() {  
  2.  var str = prompt("Please enter string");  
  3.  var searchstr = prompt("enter search string");  
  4.  var result = str.search(searchstr);  
  5.  var div = document.getElementById("content");  
  6.  div.style.color = "Blue";  
  7.  div.innerText = "Search Method \n String-> " + str + "\n search String-> " + searchstr + "\nPosition of the match-> " + result + "\n";;  
  8. }  
The following example shows how to use the search method in TypeScript. In this example, we get the string and search the string using "user.search()"; the method returns the position of the match.
 
 

Complete Program

 
Search.ts
  1. class Search_method {  
  2.  Search() {  
  3.   var str = prompt("Please enter string");  
  4.   var searchstr = prompt("enter search string");  
  5.   var result = str.search(searchstr);  
  6.   var div = document.getElementById("content");  
  7.   div.style.color = "Blue";  
  8.   div.innerText = "Search Method \n String-> " + str + "\n search String-> " + searchstr + "\nPosition of the match-> " + result + "\n";;  
  9.  }  
  10. }  
  11. window.onload = () => {  
  12.  var obj = new Search_method();  
  13.  obj.Search();  
  14. };  
Search_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="Search.js"></script>  
  9.     </head>  
  10.     <body>  
  11.         <h3>search() String Object Method In TypeScript</h3>  
  12.         <div id="content"/>  
  13.     </body>  
  14. </html>  
Search.js
  1. var Search_method = (function() {  
  2.  function Search_method() {}  
  3.  Search_method.prototype.Search = function() {  
  4.   var str = prompt("Please enter string");  
  5.   var searchstr = prompt("enter search string");  
  6.   var result = str.search(searchstr);  
  7.   var div = document.getElementById("content");  
  8.   div.style.color = "Blue";  
  9.   div.innerText = "Search Method \n String-> " + str + "\n search String-> " + searchstr + "\nPosition of the match-> " + result + "\n";;  
  10.  };  
  11.  return Search_method;  
  12. })();  
  13. window.onload = function() {  
  14.  var obj = new Search_method();  
  15.  obj.Search();  
  16. };  
Output 1
Enter a string and click on "Ok":
 
enter-string.jpg
 
Output 2
Enter a search string:
 
serach-string.jpg
  
Output 3
 
 result.jpg