Sams Teach Yourself JavaScript in 24 Hours Free Open Book

Sams Teach Yourself JavaScript in 24 Hours

Previous Page
Next Page

Working with Substrings

So far, you've worked with entire strings. JavaScript also enables you to work with substrings, or portions of a string. You can use the substring method to retrieve a portion of a string, or the charAt method to get a single character. These are explained in the following sections.

Using Part of a String

The substring method returns a string consisting of a portion of the original string between two index values, which you must specify in parentheses. For example, the following statement displays the fourth through sixth characters of the text string:

document.write(text.substring(3,6));

At this point, you're probably wondering where the 3 and the 6 come from. There are three things you need to understand about the index parameters:

  • Indexing starts with 0 for the first character of the string, so the fourth character is actually index 3.

  • The second index is noninclusive. A second index of 6 includes up to index 5 (the sixth character).

  • You can specify the two indexes in either order. The smaller one will be assumed to be the first index. In the previous example, (6,3) would have produced the same result. Of course, there is rarely a reason to use the reverse order.

As another example, suppose you defined a string called alpha to hold the alphabet:

alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

The following are examples of the substring() method using this string:

  • alpha.substring(0,4) returns ABCD.

  • alpha.substring(10,12) returns KL.

  • alpha.substring(12,10) also returns KL. Because it's smaller, 10 is used as the first index.

  • alpha.substring(6,7) returns G.

  • alpha.substring(24,26) returns YZ.

  • alpha.substring(0,26) returns the entire alphabet.

  • alpha.substring(6,6) returns the null value, an empty string. This is true whenever the two index values are the same.

Getting a Single Character

The charAt method is a simple way to grab a single character from a string. You specify the character's index, or position, in parentheses. The indexes begin at 0 for the first character. Here are a few examples using the alpha string:

  • alpha.charAt(0) returns A.

  • alpha.charAt(12) returns M.

  • alpha.charAt(25) returns Z.

  • alpha.charAt(27) returns an empty string because there is no character at that position.

Finding a Substring

Another use for substrings is to find a string within another string. One way to do this is with the indexOf method. To use this method, add indexOf to the string you want to search, and specify the string to search for in the parentheses. This example searches for "this" in the test string:

loc = test.indexOf("this");

By the Way

As with most JavaScript methods and property names, indexOf is case sensitive. Make sure you type it exactly as shown here when you use it in scripts.


The value returned in the loc variable is an index into the string, similar to the first index in the substring method. The first character of the string is index 0.

You can specify an optional second parameter to indicate the index value to begin the search. For example, this statement searches for the word fish in the temp string, starting with the 20th character:

location = temp.indexOf("fish",19);

By the Way

One use for the second parameter is to search for multiple occurrences of a string. After finding the first occurrence, you search starting with that location for the second one, and so on.


A second method, lastIndexOf(), works the same way, but finds the last occurrence of the string. It searches the string backwards, starting with the last character. For example, this statement finds the last occurrence of Fred in the names string:

location = names.lastIndexOf("Fred");

As with indexOf(), you can specify a location to search from as the second parameter. In this case, the string will be searched backward starting at that location.

Previous Page
Next Page
Index: [SYMBOL][A][B][C][D][E][F][G][H][I][J][K][L][M][N][O][P][Q][R][S][T][U][V][W][X][Y]


     Main Menu
Sams Teach Yourself JavaScript in 24 Hours
Table of Contents
Copyright
About the Author
Acknowledgments
Part I: Introducing the Concept of Web scripting and the JavaScript Language
Part II: Learning JavaScript Basics
Hour 5. Using Variables, Strings, and Arrays
Using Variables
Understanding Expressions and Operators
Data Types in JavaScript
Converting Between Data Types
Using String Objects
Working with Substrings
Using Numeric Arrays
Using String Arrays
Sorting a Numeric Array
Summary
Q&A
Quiz Questions
Quiz Answers
Exercises
Hour 6. Using Functions and Objects
Hour 7. Controlling Flow with Conditions and Loops
Hour 8. Using Built-in Functions and Libraries
Part III: Learning More About the DOM
Part IV: Working with Advanced JavaScript Features
Part V: Building Multimedia Applications with JavaScript
Part VI: Creating Complex Scripts
Part VII: Appendixes
Index


More Books
PHP Hacks
Processing Xml With Java - A Guide To Sax, Dom, Jdom, Jaxp, And Trax
The Koran (Holy Qur'an)
Macromedia Flash 8 Bible
Search Engine Optimization for Dummies
YouTube Traffic
PHP 5 for Dummies
Harry Potter and The Chamber of Secrets
Harry Potter and the Sorcerer's Stone
The Pilgrim's Progress
Wireless Hacks
Flash Hacks. 100 Industrial-Strength Tips & Tools
PayPal Hacks. 100 Industrial-Strength Tips and Tools
Amazon Hacks
Pdf Hacks
The Da Vinci Code
Google Hacks
The Holy Bible
Windows XP For Dummies
Harry Potter and the Half-Blood Prince
Seo Book
Upgrading and Repairing Networks
Macromedia Dreamweaver 8 UNLEASHED
Windows XP Annoyances
Windows XP Hacks
Microsoft Windows XP Power Toolkit
Teach Yourself MS Office In 24Hours
iPod & iTunes Missing Manual
PC Hacks 100 Industrial-Strength Tips and Tools
PC Overclocking, Optimization, and Tuning - 2th Edition
PC Hardware In A Nutshell 3rd Edition
PC Hardware in a Nutshell, 2nd Edition
Upgrading and Repairing PCs
Google for Dummies
MySQL Cookbook
Teach Yourself Macromedia Flash 8 In 24 Hours
PHP CookBook
Sams Teach Yourself JavaScript in 24 Hours
PHP5 Manual
Free Games Paper Airplanes
500 Juegos Gratis 500 Giochi Gratis 500 Jeux Gratuits 500 Jogos Gratis 500 Kostenlose Spiele