Retrieving a Portion of a String
By: aathishankaran Printer Friendly Format
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 |
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Subscribe to Tutorials
Related Tutorials
Archived Comments
1. This post is priceless. When can I find out more?
View Tutorial By: best payday loans online at 2015-11-08 05:54:19
2. Hello there! I know this is kinda off topic howeve
View Tutorial By: perumahan di semarang atas at 2016-04-10 15:39:33
3. Excellent post. I used to be checking continuously
View Tutorial By: baclofen10mg.win at 2016-10-26 09:54:45
4. I do not even understand how I ended up right here
View Tutorial By: how to find good penny stocks at 2016-12-24 03:08:14
5. Very descriptive article, I liked that a lot.Will
View Tutorial By: social media sites at 2017-04-17 00:29:22
6. Great post. I 'm facing a couple of these difficul
View Tutorial By: Santos at 2017-06-10 23:48:35
7. I needed to thank you for this excellent read!! I
View Tutorial By: Darci at 2017-06-11 21:29:36
8. ScottAgore
View Tutorial By: ScottAgore at 2017-06-18 01:56:08
9. Saved as a favorite, I actually enjoy your site!
View Tutorial By: Olga at 2017-06-19 11:23:37
10. ScottAgore
View Tutorial By: ScottAgore at 2017-06-22 19:31:35