Powered by Blogger.

Thursday, February 27, 2014

Search for Text inside Strings in javascript




Here in this article, we will discuss how we can Search for Text inside Strings in javascript.

Also check out:

- Get nth highest lowest salary in SQL Server 2008

- Constructor and Destructors in C#.Net

- Bind dropdownlist from enum in Asp.Net

When working with text strings, sometimes you need to determine if a string contains some specific substring, and if it does, you need to determine where in the string that substring occurs.

The following task searches for a substring in another string stored in a variable and displays the position where that substring is found:

1. Open a new HTML document in your preferred HTML or text editor.

2. Create the body of the document with opening and closing body tags:

<body>

</body>

3. Insert a script block in the body of the document:

<script language="JavaScript">
<!--
// -->
</script>

4. Create a variable named myVariable and assign the value "Hello Raj" to it:

var myVariable = "Hello Raj";

5. Create a second variable named therePlace and assign the results of searching for "Raj" to it:

var rajPlace = myVariable.search("Raj");

6. Display the results of the search so that the final page looks like below.

<body>
<script language="JavaScript">
<!--
var myVariable = "Hello Raj";
var therePlace = myVariable.search("Raj");
document.write(rajPlace);
// -->
</script>
</body>

7. Save the file and close it.

8. Open the file in a browser. You should see the number 6 displayed in the browser.



0 comments

Post a Comment