Exforsys

JavaScript Tutorial

  1. JavaScript Browser Objects Part 2
  2. JavaScript Frame object
  3. JavaScript Form Object
  4. JavaScript FileUpload Object
  5. JavaScript Event Object Properties and Methods
  6. JavaScript Event Object
  7. JavaScript Elements and Embed Objects
  8. JavaScript Applet Objects
  9. JavaScript Browser Objects
  10. JavaScript Object Oriented Features
  11. JavaScript Window Object Open Method Part 2
  12. JavaScript Window Object Open Method
  13. JavaScript Window Object Timeout Methods
  14. JavaScript Location Object
  15. JavaScript Location Object Properties
  16. JavaScript History Object Properties and Methods
  17. JavaScript Document Object Methods Part II
  18. JavaScript Document Object Methods Part I
  19. JavaScript Document Object Properties
  20. JavaScript Document Object
  21. JavaScript Windows Object Properties Part II
  22. JavaScript Windows Object Properties Part I
  23. JavaScript DOM Window Object
  24. Working with JavaScript DOM Objects
  25. JavaScript Array Object Methods – Part II
  26. JavaScript Array Object
  27. JavaScript Array Object Methods – Part I
  28. JavaScript Boolean Object
  29. JavaScript OnError Event
  30. JavaScript Exception Handling – Part II
  31. JavaScript Exception Handling – Part I
  32. JavaScript Event Handler
  33. JavaScript Events Handling
  34. JavaScript Array Operations
  35. JavaScript Two Dimensional Arrays
  36. Passing values to JavaScript Function
  37. JavaScript Functions
  38. JavaScript Arrays
  39. JavaScript Iterative Structures - Part II
  40. JavaScript Iterative Structures - Part I
  41. JavaScript Math Object
  42. JavaScript Date Object
  43. JavaScript String Object
  44. JavaScript Objects
  45. JavaScript Confirm Box
  46. JavaScript Alert Box
  47. JavaScript Conditional Statements Part 2
  48. JavaScript Conditional Statements Part 1
  49. How to use JavaScript in HTML page
  50. JavaScript Variables
  51. JavaScript Features
  52. JavaScript Introduction

Ads


Home arrow Technical Training arrow JavaScript Tutorial

JavaScript String Object

Author : Exforsys Inc.     Published on: 12th Aug 2007    |   Last Updated on: 6th Dec 2010

JavaScript String Object

In this JavaScript tutorial, you will learn about String Object, purpose of string object in JavaScript, purpose of string object, indexof method, lastIndexOf method and substring method along with syntax and example.

Ads

Purpose of String Object in JavaScript:

The main purpose of String Object in JavaScript is for storing text.

General method of using String Object is to declare a variable and assign a string, in other words a text to the variable.

var exf="Welcome"

assigns the text Welcome to the variable exf defined.

We have seen in earlier section that some of the methods used in String Objects are:

  • indexOf
  • lastIndexOf
  • substring()
  • toUpperCase()
  • toLowerCase()

indexOf method of String Object:

If a programmer wants to know the position of a characters or group of characters in a String Object it can be obtained by applying the indexOf method to the String.  In other words indexOf method of the String Object returns the position of the first occurrence of a specified string value in a string.

General syntax of the indexOf method of String Object in JavaScript is as follows:

indexOf(substr, [start])

Here the substr given in argument is searched in the string and if it found the index number of the searched character is or substring within the string is returned. If it is not found then -1 is returned. The default returned value is 0. The start is an optional argument specifying the position within string to begin the search.

For example:


<html>
   <
body>
      <
script type="text/javascript">
         var exf="Welcome members"
         document.write(exf.indexOf("Welcome") + "<br />")
         document.write(exf.indexOf("Members") + "<br />")

      </script>
   </
body>
</
html>

Output of the above program is:

 0
-1

This is because the first write statement has Welcome given as substring in the indexOf method and since it occurs in the starting of the String Object the value 0 is printed. The second write statement has Members given as substring in the indexOf method and since it does not occur in the string the value -1 is printed. Only members is present in the String Object and not Members so -1 is printed.

lastIndexOf method of String Object:

General syntax of lastIndexOf method of String Object is as follows:

lastIndexOf(substr, [start])

In this the substring given in parameters is searched for in the String and if it is found the index number of the searched character or substring within the string is returned. Here the order of search is from end to beginning of the String. If it is not found the value returned is -1. Start is an optional argument specifying the position within string to begin the search. Default is string.length-1.

For example

"exfor".lastIndexOf("o")

will give a value of 3.  This is because the position of text o appearing in the string is counted from the end of string and the end of string is equal to stringlenth -1 which is equal to 4 and so position of o appearing in the string is 3.

substring method of String Object:

General syntax of substring method of String Object in JavaScript is as follows:

substring(from, [to])

In this method characters in the string between from and to indexes is returned  "to" is optional, and if omitted, characters up to the end of the string is returned.

For example:


<html>
   <
body>
      <
script type="text/javascript">
         var exf="Welcome"
         document.write(exf.substring(0,3));
         document.write(exf.substring(3,0));

      </script>
   </
body>
</
html>

Output of the above program is

Wel
ome

Ads

This is because the first substring extracts text starting from the first till the second character which gives Wel as output and the second substring extracts text starting from the fourth till the end of string as the optional to is not specified.

We have seen only some of the methods of the String Object above. Apart from these, there are many other methods available for String Object like split, slice, charAt, concat, replace.



 
This tutorial is part of a JavaScript Tutorial tutorial series. Read it from the beginning and learn yourself.

JavaScript Tutorial

  1. JavaScript Browser Objects Part 2
  2. JavaScript Frame object
  3. JavaScript Form Object
  4. JavaScript FileUpload Object
  5. JavaScript Event Object Properties and Methods
  6. JavaScript Event Object
  7. JavaScript Elements and Embed Objects
  8. JavaScript Applet Objects
  9. JavaScript Browser Objects
  10. JavaScript Object Oriented Features
  11. JavaScript Window Object Open Method Part 2
  12. JavaScript Window Object Open Method
  13. JavaScript Window Object Timeout Methods
  14. JavaScript Location Object
  15. JavaScript Location Object Properties
  16. JavaScript History Object Properties and Methods
  17. JavaScript Document Object Methods Part II
  18. JavaScript Document Object Methods Part I
  19. JavaScript Document Object Properties
  20. JavaScript Document Object
  21. JavaScript Windows Object Properties Part II
  22. JavaScript Windows Object Properties Part I
  23. JavaScript DOM Window Object
  24. Working with JavaScript DOM Objects
  25. JavaScript Array Object Methods – Part II
  26. JavaScript Array Object
  27. JavaScript Array Object Methods – Part I
  28. JavaScript Boolean Object
  29. JavaScript OnError Event
  30. JavaScript Exception Handling – Part II
  31. JavaScript Exception Handling – Part I
  32. JavaScript Event Handler
  33. JavaScript Events Handling
  34. JavaScript Array Operations
  35. JavaScript Two Dimensional Arrays
  36. Passing values to JavaScript Function
  37. JavaScript Functions
  38. JavaScript Arrays
  39. JavaScript Iterative Structures - Part II
  40. JavaScript Iterative Structures - Part I
  41. JavaScript Math Object
  42. JavaScript Date Object
  43. JavaScript String Object
  44. JavaScript Objects
  45. JavaScript Confirm Box
  46. JavaScript Alert Box
  47. JavaScript Conditional Statements Part 2
  48. JavaScript Conditional Statements Part 1
  49. How to use JavaScript in HTML page
  50. JavaScript Variables
  51. JavaScript Features
  52. JavaScript Introduction
 

Comments