Retrieving a Portion of a String

By: aathishankaran Emailed: 1654 times Printed: 2124 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: 

"OO7".charAt(20102)

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

anchor("anchorName")

"Section3".anchor

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

big()

"Just save the name". big()

<BIG>Just save the name</BIG> 

Blink()

“Why ask why".blink()

<BLlNK>Why ask why</BLlNK> 

Boid()

"Liveandlearn".bold()

<B>Liveandlearn</B>

Fixed()

"JavaScriptcode".fixed()

<TT>JavaScript code</TT>

fontcolor("color")

"JavaPrograms",fontcolor("green")

<FONTCOLOR"green">JavaPrgrams</FONT>

fontsize(size)

"Impact".fontsize(3)

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

Italics()

"Java Script Unleashed".italics()

<I>JavaScript Unleashed</I>

Link("URL")

"See My Home Page". link("http://www.myhomepage.com/”)

<A HREF="http//www.myhomepage.com/> SeeMyHomePage</A>

Small()

"TinyTim".small()

<SMALL>Tiny Tim</SMALL>

Strike()

"GameCanceled"strike()

<STRIKE>GameCanceled</STRIKE>

Sub()

"Submarines".sub()

<SUB>Submarines</SUB>

sup()

"Superman".sup()

<SUP>Superman</SUP>

toLowerCase()

"SeeSPOTrUN".toLowerCase()

seespotrun

ToUpperCase()

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

LET'S GO TO THE BEACH

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. 

Symbol

Description 

\t

Tab

\n

New line

\r

Carriage return

\f

Form feed

\\

Backslash

\b

Backspace

\"

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

Comments(0)


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 )
JSP Example to connect to MS SQL database and retrieve records
What are the different scopes in JSP?
JSP Program for display Date
JSP CheckBox Example
Automatically Refreshing a JSP
JSP Alert Example
Sending Email using JSP
Writing your first JSP page
Uploading an Image to a Database using JSP
Comparison operators in JSP
The Advantages of JSP
Uploading a file to a server using JSP
Tags using in jsp
A JSP page that gets properties from a bean
The taglib, tag, include, attribute and the variable Directive in JSP
Most Emailed Articles (in JSP)
Steps to get a Free SSL certificate for your Tomcat
What is JSP?
Retrieving a Portion of a String
Tag libraries
Getting HTTP Request Headers in a JSP
Tags using in jsp
Built-in Object in Javascript
Closing Windows
Math Object
LifecycleException: service.getName(): "Catalina"; Protocol handler start failed: `java.net.BindException: Permission denied <null>:80
What is JSP?
Click to Activate and Use this control
Radio Object
Frame Object
Referencing Windows