↑
Main Page
ECMAScript provides
In this code, the string
“yellow”
is compared to three different values,
“brick”
,
“yellow”
, and
“zoo”
.
Because
“brick”
comes alphabetically before
“yellow”
,
localCompare()
returns 1;
“yellow”
is
equal to
“yellow”
, so
localCompare()
returns 0 for that line;
“zoo”
comes after
“yellow”
, so
localCompare()
returns –1. Once again, because the values are implementation-specific, it is best to
use
localCompare()
in this way:
var oStringObject1 = new String(“yellow”);
var oStringObject2 = new String(“brick”);
var iResult = sTestString.localeCompare(“brick”);
if(iResult < 0) {
alert(oStringObject1 + “ comes before “ + oStringObject2);
} else if (iResult > 0) {
alert(oStringObject1 + “ comes after “ + oStringObject2);
} else {
alert(“The two strings are equal”);
}
By using this sort of construct, you can be sure that the code works correctly in all implementations.
The unique part of
localeCompare()
is that an implementation’s locale (country and language) indi-
cates exactly how this method operates. In the United States, where English is the standard language for
ECMAScript implementations,
localCompare()
is case-sensitive, determining that uppercase letters
come alphabetically after lowercase letters. However, this may not be the case in other locales.
ECMAScript provides two methods for creating string values from a substring:
slice()
and
sub-
string()
. Both methods return a substring of the string they act on, and both accept either one or two
arguments. The first argument is the position where capture of the substring begins; the second argu-
ment, if used, is the position
before
which capture is stopped (which is to say that the character at this
position is not included in the returned value). If the second argument is omitted, it is assumed that the
ending position is the length of the string. Just as with the
concat()
method,
slice()
and
sub-
string()
do not alter the value of the
String
object itself: They simply return a primitive String value
as the result, leaving the
String
object unchanged.
var oStringObject = new String(“hello world”);
alert(oStringObject.slice(3)); //outputs “lo world”
alert(oStringObject.substring(3)); //outputs “lo world”
alert(oStringObject.slice(3, 7)); //outputs “lo w”
alert(oStringObject.substring(3,7)); //outputs “lo w”
In this example,
slice()
and
substring()
are used in the same manner and, ironically enough, return
the same values. When given just one argument,
3
, both methods return
“lo world”
, as the second
“l”
in
“hello”
is in position 3. When given two arguments,
3
and
7
, both methods return
“lo w”
(the
“o”
in
“world”
is in position 7, so it is not included). Why have two methods that do the exact same thing?
Truthfully, the methods aren’t identical, but they differ only in how they deal with arguments that are
negative numbers.
For the
slice()
method, a negative argument is treated as the length of the string plus the negative
argument; the
substring()
method treats a negative argument as 0 (which means that it is ignored).
For example:
var oStringObject= new String(“hello world”);
alert(oStringObject.slice(-3)); //outputs “rld”
31
ECMAScript Basics
05_579088 ch02.qxd 3/28/05 11:35 AM Page 31
Free JavaScript Editor
Ajax Editor
©
→ R7