Introduction
In this article we will learn various string object methods in JavaScript. As we know JavaScript is an object oriented language and everything is treated as an object in JavaScript. String is also one type of object in JavaScript.
Method |
Description |
indexOf() |
Returns the position of the first found occurrence of a specified value in a string |
charAt() |
Returns the character at the specified index |
replace() |
Searches for a match between a substring (or regular expression) and a string, and replaces the matched substring with a new substring |
substring() |
Extracts the characters from a string, between two specified indices |
toLowerCase() |
Converts a string to lowercase letters |
toUpperCase() |
Converts a string to uppercase letters |
Let us understand one by one with an example.
Index of Function
The indexOf function returns the position of the first occurrence of a specified value in a string. In the following example we are trying to find the index of "s" in the "a" string.
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
-
- </head>
- <body>
- <form id="form1" runat="server">
- <script>
- var a = "RamaSagar";
- alert(a.indexOf('S'));
- </script>
- </form>
- </body>
- </html>
Output
charAt() Function
This function returns the character at a specific position. It takes one integer argument specifying an index of the string. Here is a code snippet to understand the charAt() function.
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
-
- </head>
- <body>
- <form id="form1" runat="server">
- <script>
- var a = "RamaSagar";
- alert(a.charAt(2));
- </script>
- </form>
- </body>
- </html>
Output
Replace() Function
This function replaces a part of a string with some other string. It uses pattern matching to replace the part of the string. In this example string we are replacing “Rama” by the string “Replaced”.
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
-
- </head>
- <body>
- <form id="form1" runat="server">
- <script>
- var a = "RamaSagar";
- alert(a.replace("Rama","Replaced"));
- </script>
- </form>
- </body>
- </body>
- </html>
Output
substring() Function
The substring function gets the characters from a string between two specified indices.
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
-
- </head>
- <body>
- <form id="form1" runat="server">
- <script>
- var a = "RamaSagar";
- alert(a.substring(2,5));
- </script>
- </form>
- </body>
- </body>
- </html>
Output
toLowerCase() Function
This function changes the string to lower case from upper case. In the example string there are two upper case characters and by using this function all characters are changed into lowercase.
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
-
- </head>
- <body>
- <form id="form1" runat="server">
- <script>
- var a = "RAMASAGAR";
- alert(a.toLowerCase());
- </script>
- </form>
- </body>
- </body>
- </html>
toUpperCase() Function
This function converts the string to uppercase. The following is a sample example of the toUpperCase() function:
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
-
- </head>
- <body>
- <form id="form1" runat="server">
- <script>
- var a = "ramasagar";
- alert(a.toUpperCase());
- </script>
- </form>
- </body>
- </body>
- </html>
Summary
In this article we learned various functions of strings in JavaScript. In a future article we will learn more basic concepts of JavaScript.
Reference
http://www.w3schools.com/jsref/jsref_obj_string.asp