Retrieving a Portion of a String

By: aathishankaran Emailed: 1788 times Printed: 2615 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

You can retrieve a portion of a string variable or literal by using the substring() method. It has two parameters-the start position and end position of the substring you want to return. Just like indexOf() and lastIndexOf(), this method is also zero-based, such that the first position of the string begins at 0. For example, the following code returns New: 

"New England".substring(O,3) 

If you want to retrieve a single character, you can use charAt(). The charAt() method returns the character at the position you specify as a parameter. For example, the following code re-turns a value of v:

                "Denver Broncos".charAt(3) 

If you specify a position that is out of the range of the string, a value of -1 is returned: 


Formatting Strings 

JavaScript has several methods you can use to format strings. Most of these methods are simply equivalents of HTML formatting tags. Using the formatting methods gives you an object-oriented way of dealing with HTML formatting tags, and it's also easier than continuously concatenating HTML tags to strings. In other words, if you have a string literal "This is the day" and you want to add bold formatting to it, you could do so using one of two means. 

First, you could add HTML formatting tags: 

"<B>" + "This is the day" + "</B>"  

Second, you could use the String object method bold () to add the formatting:

"This is the day".  

When you use this string, the following HTML text is returned: 

"<B> This is the day</B>"  

If you have a string variable called dayVar that contains the "This is the day" text, you could use the same two options: 

dayVar = "This is the day" "<B>" + dayVar + "</B>"  

The preceding code accomplishes the same thing as the following: 

dayVar = "This is the day" dayVar.bold()  

Keep in mind the context in which you can use the formatting methods. You cannot use them outside a JavaScript script as a substitute for HTML formatting tags. 

String Method Example Returns HTML Code



<A NAME="Section3">Section('Section3")3</A>


"Just save the name". big()

<BIG>Just save the name</BIG> 


“Why ask why".blink()

<BLlNK>Why ask why</BLlNK> 






<TT>JavaScript code</TT>






<FONT SIZE="3">lmpact</FONT>


"Java Script Unleashed".italics()

<I>JavaScript Unleashed</I>


"See My Home Page". link("”)

<A HREF="http//> SeeMyHomePage</A>

















"let's go to the BEACH".toUpperCase()


Working with Special Characters 

When working with strings in any language, you will discover certain characters that are difficult to use. JavaScript enables you to work with these special case characters by using a backslash character (\) followed by the character or its code. Table 14.3 lists the JavaScript in line symbols. 






New line


Carriage return


Form feed






Double quote


Single quote


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: ` 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 )
JSP Example to connect to MS SQL database and retrieve records
What are the different scopes in JSP?
JSP CheckBox Example
JSP Alert Example
Comparison operators in JSP
Sending Email using JSP
Uploading an Image to a Database using JSP
Arithmetic Evaluation Using the Expression Language in JSP
The Advantages of JSP
Disabling Scriptlets in JSP using web.xml
The Request Object in JSP
Writing your first JSP page
Uploading a file to a server using JSP
Deploying an Individual JSP on Tomcat
Automatically Refreshing a JSP
Most Emailed Articles (in JSP)
Calling JSP from a batch file
Simple HTML-Building Utilities
Handling Events
The Tag Life Cycle with Attributes in JSP
JSP Program for display Date
Sessions in JSP
The Advantages of JSP
Using a DataSource from WebLogic in a JSP
The JSP Program running first Time.
JSP pages in servlet
Combining Scriptlets with HTML
Declaring variable in JSP
An Example Using Servlet Initialization and Page Modification Dates
Password Object