Built-in Object in Javascript

By: aathishankaran Emailed: 1768 times Printed: 2516 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

String Object 

Strings area fundamental part of any programming language. Strings, which are a set of alpha-numeric characters, can be either a literal string, such as "Push the envelope" or a variable representing a string, such as the Phrase. A string can also be treated as an object, complete with its own suite of methods and properties. 

Creating a String Object. 

Use the following procedure to create a string object. 

Var str_obj =new String([message])


<SCRIPT LANGUAGE = "JavaScript">

var str = new String("My name is Richard" )

alert (str. length)


Working with Strings 

Because strings are one of the primary types of data you have to work with, it is critical to have some way of extracting data from strings and obtaining information about strings. JavaScript has the properties and methods 





Returns an integer representing the value of size of the string.

charAt (pos)

Returns the character at the specified index. 

indexOf (searchText [,startPos])

Returns the index of the first occurrence of SearchText.

lastIndexOf(searchText [,endPos])

Returns the index of last occurrence of searchText

substring (startPos, endPos)

Returns the substring of the string starting at startpos and ending at endPos

 Determining String Lengths 

You can use the String object's length property to determine the size of a string.

For example, the following code returns 17: 

"Crazy Legs Nelson".length 

The following code returns 16: 

var str = "This is the day." len = str.length 

Searching Within Strings 

You can search for text within strings by using indexOf() and lastIndexOf(). Use these methods when you want to search for a particular character or substring within a string and return the position (or index) of the occurrence within the string. Whereas indexOf() starts at the left of the string and moves right, lastIndexOfO does the same operation but starts at the left. Both indexOf() and lastIndexOfO start at the 0 position for the first character encountered, and both return a value of -1 if the search text is not found. For example, the following code re-turns a value of 3: 

"Time and time again".indexOf ("e") 

On the other hand, the following code returns a value of 12: 

"Time and time again".lastIndexOf("e") 

Both methods have an optional second parameter that enables you to specify here in the string you want to start the search. For example, the script shown n Listing 14.1 searches through the variable graf and counts the number of occurrences of the letter 'e'. 

<SCRIPT LANGUAGE = "JavaScript">

pos = 0 num = -1 i = -1

var graf = "While nearly everyone agrees on the principle of reuse,

the priority we give it varies wildly."

while (pos != -1)


pos = graf.indexOf("e",i+1)

num += 1

i = pos




document.write("There were" + num + " e' s in that paragraph.")


JSP Home | All JSP Tutorials | Latest JSP Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.

Bookmark and Share


Be the first one to add a comment

Your name (required):

Your email(required, will not be shown to the public):

Your sites URL (optional):

Your comments:

More Tutorials by aathishankaran
Web Security Issues
The Web User's Perspective
Server-side plug-Ins
The best way to avoid security vulnerabilities with new server
JavaScript Security
Window Object
Working with Status Bar Messages
Retrieving a Portion of a String
Referencing Windows
Math Object
Frame Object
Document Object
Closing Windows
Built-in Object in Javascript
Textarea Object

More Tutorials in JSP
LifecycleException: service.getName(): "Catalina"; Protocol handler start failed: `java.net.BindException: Permission denied <null>:80
JSP Alert Example
JSP CheckBox Example
Uploading an Image to a Database using JSP
Uploading a file to a server using JSP
A JSP page that gets properties from a bean
The page Directive in JSP
The taglib, tag, include, attribute and the variable Directive in JSP
Declarations in JSP
Scriptlets and Expressions in JSP
Tag Libraries in JSP
The Request Object in JSP
The Response Object in JSP
The Out Object in JSP
The Session Object in JSP

More Latest News
Most Viewed Articles (in JSP )
What are the different scopes in JSP?
JSP Example to connect to MS SQL database and retrieve records
Sending Email using JSP
Automatically Refreshing a JSP
LifecycleException: service.getName(): "Catalina"; Protocol handler start failed: `java.net.BindException: Permission denied <null>:80
Click to Activate and Use this control
The JSP Program running first Time.
Embedding java codes in jsp sciptlets
JSP pages in servlet
JSP Program for display Date
Protecting your website with a login page
The Advantages of Servlets Over “Traditional” CGI
The Servlet Life Cycle
Built-in Object in Javascript
Most Emailed Articles (in JSP)
What is JSP?
Declaring variable in JSP
Techniques for form editing
A Simple Servlet Generating Plain Text
An Example Using Servlet Initialization and Page Modification Dates
Text Object
The Basic Syntax Expression Language in JSP
Disabling Scriptlets in JSP using web.xml
The BodyTag Interface in JSP
What are the different scopes in JSP?
Sending Email using JSP
Using a DataSource from WebLogic in a JSP
Getting HTTP Request Headers in a JSP