Retrieving a Portion of a String

By: aathishankaran  

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


Archived Comments

1. ScottAgore
View Tutorial          By: ScottAgore at 2017-06-22 19:31:35

2. Saved as a favorite, I actually enjoy your site!

Take a look at my web page .

View Tutorial          By: Olga at 2017-06-19 11:23:37

3. ScottAgore
View Tutorial          By: ScottAgore at 2017-06-18 01:56:08

4. I needed to thank you for this excellent read!! I definitely loving every small
bit of it I

View Tutorial          By: Darci at 2017-06-11 21:29:36

5. Great post. I 'm facing a couple of these difficulties.

Feel free to visit my weblog:

View Tutorial          By: Santos at 2017-06-10 23:48:35

6. Very descriptive article, I liked that a lot.Will there be
a part 2?

View Tutorial          By: social media sites at 2017-04-17 00:29:22

7. I do not even understand how I ended up right here, however I believed this post was great.

View Tutorial          By: how to find good penny stocks at 2016-12-24 03:08:14

8. Excellent post. I used to be checking continuously this weblog and I'm inspired!


View Tutorial          By: at 2016-10-26 09:54:45

9. Hello there! I know this is kinda off topic however I'd figured I'd ask.
Would you be interes

View Tutorial          By: perumahan di semarang atas at 2016-04-10 15:39:33

10. This post is priceless. When can I find out more?
View Tutorial          By: best payday loans online at 2015-11-08 05:54:19

Most Viewed Articles (in JSP )

Latest Articles (in JSP)

Comment on this tutorial