String

Unit 22

String objects

A String object has two properties.
The length property comes from the String() constructor. The constructor property comes from the Object() constructor.

Character methods

String objects have several methods that use a character.

Substring methods

String objects have several methods that deal with substrings.

Case and string methods

There are String methods to produce upper or lower case. There are string methods that just return the primitive string.

Strings and Arrays

The String method split() converts a String into an Array.
The Array method join() converts an Array into a String.
We previously saw these in the unit on Arrays.

Regular expressions

The string methods match() search() replace() use regular expressions. We will see only simple examples here.

One of the key techniques for processing strings is to use regular expressions. Regular expressions are used in JavaScript, Perl, several Unix utilities, and other environments that process strings.
I recommend you take the CIS 18B Advanced UNIX/LINUX, which teaches Regular Expressions.

After you learn regular expressions, I recommend you read about these methods in Flanagan's book.
We have come to the point where you can begin to read Flanagan's book. If you plan to continue using JavaScript, I recommend you buy a copy. It is kept up to date. It is precise and detailed. It has four main sections, all on JavaScript: 1) core discussion 2) html/xml/browser discussion 3) core reference 4) html/xml/browser reference.
JavaScript , The Definitive Guide, Fifth Edition by David Flanagan, 2006, O'Reilly ISBN-13: 978-0-596-10199-2

Compare

There is a localCompare() method. It can be used to support alphabets other than are used in American English. We can compare strings using the comparison operators:
"aa" < "ab"   "ac" > "ab"   "ab" == "ab"
are all true.

HTML node methods

This section is optional. It shows some dynamic HTML node methods.

There are some non-standard String methods that will put a string within an HTML element. They are the following:
anchor() big() blink() bold() fixed() fontcolor() fontsize() italics() link() small() strike() sub() sup()
I suggest you may not want to use these.

Standard methods for managing HTML nodes include:
document.getElementById() document.createElement document.createTextNode() Node.appendChild() Node.removeChild()

A very well supported, and very popular non-standard way of managing HTML nodes is to modify the Node.innerHTML property.

The standard methods, and modification of innerHTML are shown in the sample.

Reading assignment

Reading assignments are in the text book, Java Script, A Beginner's Guide, Second Edition, by John Pollock; McGraw Hill / Osborne, ISBN 0-07-222790-7

Read Module 13.
I suggest you omit Module 15.
Optionally, you may look at regular expressions in Module 16, section 16.2, Using Regular Expressions.
Optionally, you may look at other things in Module 16.

Alternate reading assignments are in the text book, Java Script Concepts & Techniques Programming Interactive Web Sites, by Tina Spain McDuffie; Franklin, Beedle & Associates, ISBN 1-887902-45-7

Read Chapter 9 Manipulating strings.

Lecture notes

Do NOT read the lecture notes before hearing the lecture. If you do, you will find the lecture very boring. Read the lecture notes if you do not attend the lecture, or if you wish to review the material.