Core object | |
Implemented in |
Navigator 2.0: Create a String object only by quoting characters.Navigator 3.0, LiveWire 1.0: added String constructor; added prototype property; added split method; added ability to pass strings among scripts in different windows or frames (in previous releases, you had to add an empty string to another window's string to refer to it)Navigator 4.0, Netscape Server 3.0: added concat , match , replace , search , slice , and substr methods.
|
String
constructor:new String(string);
string | Any string. |
String
object is a built-in JavaScript object. You an treat any JavaScript string as a String
object.A string can be represented as a literal enclosed by single or double quotation marks; for example, "Netscape" or 'Netscape'.
| Reflects the length of the string. |
|
Allows the addition of properties to a String object.
|
var last_name = "Schaefer"Example 2: String object properties. The following statements evaluate to 8,
"SCHAEFER,"
and "schaefer"
:last_name.lengthExample 3: Accessing individual characters in a string. You can think of a string as an array of characters. In this way, you can access the individual characters in the string by indexing that array. For example, the following code:
last_name.toUpperCase()
last_name.toLowerCase()
var myString = "Hello"displays "The first character in the string is H"
document.write ("The first character in the string is " + myString[0])
Example 4: Pass a string among scripts in different windows or frames. The following code creates two string variables and opens a second window:
var lastName = new String("Schaefer")If the HTML source for the second window (
var firstName = new String ("Jesse")
empWindow=window.open('string2.html','window1','width=300,height=300')
string2.html
) creates two string variables, empLastName
and empFirstName
, the following code in the first window assigns values to the second window's variables:empWindow.empFirstName=firstNameThe following code in the first window displays the values of the second window's variables:
empWindow.empLastName=lastName
alert('empFirstName in empWindow is ' + empWindow.empFirstName)
alert('empLastName in empWindow is ' + empWindow.empLastName)
Property of |
String
|
Read-only | |
Implemented in | Navigator 2.0, LiveWire 1.0 |
var x="Netscape"
alert("The string length is " + x.length)
Function.prototype
.
Property of |
String
|
Implemented in | Navigator 3.0, Netscape Server 3.0 |
Method of |
String
|
Implemented in | Navigator 2.0, LiveWire 1.0 |
anchor(nameAttribute)
nameAttribute | A string. |
anchor
method with the document.write
or document.writeln
methods to programmatically create and display an anchor in a document. Create the anchor with the anchor
method, and then call write
or writeln
to display the anchor in a document. In server-side JavaScript, use the write
function to display the anchor.
Anchors created with the anchor
method become elements in the document.anchors
array.
msgWindow
window and creates an anchor for the table of contents:var myString="Table of Contents"The previous example produces the same output as the following HTML:
msgWindow.document.writeln(myString.anchor("contents_anchor"))
<A NAME="contents_anchor">Table of Contents</A>In server-side JavaScript, you can generate this HTML by calling the
write
function instead of using document.writeln
.String.link
BIG
tag.
Method of |
String
|
Implemented in | Navigator 2.0, LiveWire 1.0 |
big()
big
method with the write
or writeln
methods to format and display a string in a document. In server-side JavaScript, use the write
function to display the string.string
methods to change the size of a string:var worldString="Hello, world"
document.write(worldString.small())The previous example produces the same output as the following HTML:
document.write("<P>" + worldString.big())
document.write("<P>" + worldString.fontsize(7))
<SMALL>Hello, world</SMALL>
<P><BIG>Hello, world</BIG>
<P><FONTSIZE=7>Hello, world</FONTSIZE>
String.fontsize
, String.small
BLINK
tag.
Method of |
String
|
Implemented in | Navigator 2.0, LiveWire 1.0 |
blink()
blink
method with the write
or writeln
methods to format and display a string in a document. In server-side JavaScript, use the write
function to display the string.string
methods to change the formatting of a string:var worldString="Hello, world"
document.write(worldString.blink())The previous example produces the same output as the following HTML:
document.write("<P>" + worldString.bold())
document.write("<P>" + worldString.italics())
document.write("<P>" + worldString.strike())
<BLINK>Hello, world</BLINK>
<P><B>Hello, world</B>
<P><I>Hello, world</I>
<P><STRIKE>Hello, world</STRIKE>
String.bold
, String.italics
, String.strike
B
tag.
Method of |
String
|
Implemented in | Navigator 2.0, LiveWire 1.0 |
bold()
bold
method with the write
or writeln
methods to format and display a string in a document. In server-side JavaScript, use the write
function to display the string.string
methods to change the formatting of a string:var worldString="Hello, world"The previous example produces the same output as the following HTML:
document.write(worldString.blink())
document.write("<P>" + worldString.bold())
document.write("<P>" + worldString.italics())
document.write("<P>" + worldString.strike())
<BLINK>Hello, world</BLINK>
<P><B>Hello, world</B>
<P><I>Hello, world</I>
<P><STRIKE>Hello, world</STRIKE>
String.blink
, String.italics
, String.strike
Method of |
String
|
Implemented in | Navigator 2.0, LiveWire 1.0 |
charAt(index)
index | An integer between 0 and 1 less than the length of the string. |
stringName
is stringName.length - 1
. If the index
you supply is out of range, JavaScript returns an empty string."Brave new world"
:var anyString="Brave new world"
document.writeln("The character at index 0 is " + anyString.charAt(0))These lines display the following:
document.writeln("The character at index 1 is " + anyString.charAt(1))
document.writeln("The character at index 2 is " + anyString.charAt(2))
document.writeln("The character at index 3 is " + anyString.charAt(3))
document.writeln("The character at index 4 is " + anyString.charAt(4))
The character at index 0 is B
The character at index 1 is r
The character at index 2 is a
The character at index 3 is v
The character at index 4 is e
In server-side JavaScript, you can display the same output by calling the write
function instead of using document.write
.
String.indexOf
, String.lastIndexOf
, String.split
Method of |
String
|
Implemented in | Navigator 4.0, Netscape Server 3.0 |
charCodeAt(index)
index | (Optional) An integer between 0 and 1 less than the length of the string. The default value is 0. |
"ABC".charCodeAt(0)
Method of |
String
|
Implemented in | Navigator 4.0, Netscape Server 3.0 |
concat(string2)
string1 | The first string. |
string2 | The second string. |
concat
combines the text from two strings and returns a new string. Changes to the text in one string do not affect the other string. <SCRIPT>This writes:
str1="The morning is upon us. "
str2="The sun is bright."
str3=str1.concat(str2)
document.writeln(str1)
document.writeln(str2)
document.writeln(str3)
</SCRIPT>
The morning is upon us.
The sun is bright.
The morning is upon us. The sun is bright.
TT
tag.
Method of |
String
|
Implemented in | Navigator 2.0, LiveWire 1.0 |
fixed()
fixed
method with the write
or writeln
methods to format and display a string in a document. In server-side JavaScript, use the write
function to display the string.fixed
method to change the formatting of a string:var worldString="Hello, world"The previous example produces the same output as the following HTML:
document.write(worldString.fixed())
<TT>Hello, world</TT>
<
FONT COLOR=color>
tag.
Method of |
String
|
Implemented in | Navigator 2.0, LiveWire 1.0 |
fontcolor(color)
color | A string expressing the color as a hexadecimal RGB triplet or as a string literal. String literals for color names are listed in Appendix B, "Color Values," in the JavaScript Guide. |
fontcolor
method with the write
or writeln
methods to format and display a string in a document. In server-side JavaScript, use the write
function to display the string.
The fontcolor
method overrides a value set in the fgColor
property.
fontcolor
method to change the color of a string:var worldString="Hello, world"
document.write(worldString.fontcolor("maroon") +
" is maroon in this line")
document.write("<P>" + worldString.fontcolor("salmon") +
" is salmon in this line")
document.write("<P>" + worldString.fontcolor("red") +
" is red in this line")
document.write("<P>" + worldString.fontcolor("8000") +The previous example produces the same output as the following HTML:
" is maroon in hexadecimal in this line")
document.write("<P>" + worldString.fontcolor("FA8072") +
" is salmon in hexadecimal in this line")
document.write("<P>" + worldString.fontcolor("FF00") +
" is red in hexadecimal in this line")
<FONT COLOR="maroon">Hello, world</FONT> is maroon in this line
<P><FONT COLOR="salmon">Hello, world</FONT> is salmon in this line
<P><FONT COLOR="red">Hello, world</FONT> is red in this line
<FONT COLOR="8000">Hello, world</FONT>
is maroon in hexadecimal in this line
<P><FONT COLOR="FA8072">Hello, world</FONT>
is salmon in hexadecimal in this line
<P><FONT COLOR="FF00">Hello, world</FONT>
is red in hexadecimal in this line
<
FONT SIZE=size>
tag.
Method of |
String
|
Implemented in | Navigator 2.0, LiveWire 1.0 |
fontsize(size)
size | An integer between 1 and 7, a string representing a signed integer between 1 and 7. |
fontsize
method with the write
or writeln
methods to format and display a string in a document. In server-side JavaScript, use the write
function to display the string.string
methods to change the size of a string:var worldString="Hello, world"
document.write(worldString.small())The previous example produces the same output as the following HTML:
document.write("<P>" + worldString.big())
document.write("<P>" + worldString.fontsize(7))
<SMALL>Hello, world</SMALL>
<P><BIG>Hello, world</BIG>
<P><FONTSIZE=7>Hello, world</FONTSIZE>
String.big
, String.small
Method of |
String
|
Static | |
Implemented in | Navigator 4.0, Netscape Server 3.0 |
fromCharCode(num1, ..., numN)
num1, ..., numN | A sequence of numbers that are ISO-Latin-1 codeset values. |
String
object.
Because fromCharCode
is a static method of String
, you always use it as String.fromCharCode()
, rather than as a method of a String
object you created.
String.fromCharCode(65,66,67)Example 2. The
which
property of the KeyDown
, KeyPress
, and KeyUp
events contains the ASCII value of the key pressed at the time the event occurred. If you want to get the actual letter, number, or symbol of the key, you can use fromCharCode
. The following example returns the letter, number, or symbol of the KeyPress event's which
property. String.fromCharCode(KeyPress.which)
String
object of the first occurrence of the specified value, starting the search at fromIndex
, or -1 if the value is not found.
Method of |
String
|
Implemented in | Navigator 2.0, LiveWire 1.0 |
indexOf(searchValue, fromIndex)
stringName
is stringName.length - 1
.
If stringName
contains an empty string (""
), indexOf
returns an empty string.
The indexOf
method is case sensitive. For example, the following expression returns -1:
"Blue Whale".indexOf("blue")
indexOf
and lastIndexOf
to locate values in the string "Brave new world."
var anyString="Brave new world"
//Displays 8Example 2. The following example defines two string variables. The variables contain the same string except that the second string contains uppercase letters. The first
document.write("<P>The index of the first w from the beginning is " +
anyString.indexOf("w"))
//Displays 10
document.write("<P>The index of the first w from the end is " +
anyString.lastIndexOf("w"))
//Displays 6
document.write("<P>The index of 'new' from the beginning is " +
anyString.indexOf("new"))
//Displays 6
document.write("<P>The index of 'new' from the end is " +
anyString.lastIndexOf("new"))
writeln
method displays 19. But because the indexOf
method is case sensitive, the string "cheddar"
is not found in myCapString
, so the second writeln
method displays -1.myString="brie, pepper jack, cheddar"Example 3. The following example sets
myCapString="Brie, Pepper Jack, Cheddar"
document.writeln('myString.indexOf("cheddar") is ' +
myString.indexOf("cheddar"))
document.writeln('<P>myCapString.indexOf("cheddar") is ' +
myCapString.indexOf("cheddar"))
count
to the number of occurrences of the letter x
in the string str
:count = 0;
pos = str.indexOf("x");
while ( pos != -1 ) {
count++;
pos = str.indexOf("x",pos+1);
}
String.charAt
, String.lastIndexOf
, String.split
I
tag.
Method of |
String
|
Implemented in | Navigator 2.0, LiveWire 1.0 |
italics()
italics
method with the write
or writeln
methods to format and display a string in a document. In server-side JavaScript, use the write
function to display the string.string
methods to change the formatting of a string:var worldString="Hello, world"
document.write(worldString.blink())The previous example produces the same output as the following HTML:
document.write("<P>" + worldString.bold())
document.write("<P>" + worldString.italics())
document.write("<P>" + worldString.strike())
<BLINK>Hello, world</BLINK>
<P><B>Hello, world</B>
<P><I>Hello, world</I>
<P><STRIKE>Hello, world</STRIKE>
String.blink
, String.bold
, String.strike
String
object of the last occurrence of the specified value. The calling string is searched backward, starting at fromIndex
, or -1 if not found.
Method of |
String
|
Implemented in | Navigator 2.0, LiveWire 1.0 |
lastIndexOf(searchValue, fromIndex)
stringName
.length - 1.
The lastIndexOf
method is case sensitive. For example, the following expression returns -1:
"Blue Whale, Killer Whale".lastIndexOf("blue")
indexOf
and lastIndexOf
to locate values in the string "Brave new world."
var anyString="Brave new world"
//Displays 8In server-side JavaScript, you can display the same output by calling the
document.write("<P>The index of the first w from the beginning is " +
anyString.indexOf("w"))
//Displays 10
document.write("<P>The index of the first w from the end is " +
anyString.lastIndexOf("w"))
//Displays 6
document.write("<P>The index of 'new' from the beginning is " +
anyString.indexOf("new"))
//Displays 6
document.write("<P>The index of 'new' from the end is " +
anyString.lastIndexOf("new"))
write
function instead of using document.write
.String.charAt
, String.indexOf
, String.split
Method of |
String
|
Implemented in | Navigator 2.0, LiveWire 1.0 |
link(hrefAttribute)
hrefAttribute |
Any string that specifies the HREF attribute of the A tag; it should be a valid URL (relative or absolute).
|
link
method to programmatically create a hypertext link, and then call write
or writeln
to display the link in a document. In server-side JavaScript, use the write
function to display the link.
Links created with the link
method become elements in the links
array of the document
object. See document.links
.
var hotText="Netscape"
var URL="http://home.netscape.com"
document.write("Click to return to " + hotText.link(URL))The previous example produces the same output as the following HTML:
Click to return to <A HREF="http://home.netscape.com">Netscape</A>
Anchor
Method of |
String
|
Implemented in | Navigator 4.0 |
match(regexp)
regexp | Name of the regular expression. It can be a variable name or a literal. |
g
(for global) and i
(for ignore case) flags in the regular expression. These can be included separately or together. The following two examples below show how to use these flags with match
. String.search
or the regular expression test
method. match
is used to find 'Chapter' followed by 1 or more numeric characters followed by a decimal point and numeric character 0 or more times. The regular expression includes the i
flag so that case will be ignored. <SCRIPT>This returns the array containing Chapter 3.4.5.1,Chapter 3.4.5.1,.1
str = "For more information, see Chapter 3.4.5.1";
re = /(chapter \d+(\.\d)*)/i;
found = str.match(re);
document.write(found);
</SCRIPT>
'Chapter 3.4.5.1'
is the first match and the first value remembered from (Chapter \d+(\.\d)*)
.
'.1'
is the second value remembered from (\.\d)
.
<SCRIPT>The returned array contains D, d.
str = "abcDdcba";
newArray = str.match(/d/gi);
document.write(newArray);
</SCRIPT>
Method of |
String
|
Implemented in | Navigator 4.0 |
replace(regexp, newSubStr)
String
object it is called on; it simply returns a new string.
If you want to execute a global search and replace, or a case insensitive search, include the g
(for global) and i
(for ignore case) flags in the regular expression. These can be included separately or together. The following two examples below show how to use these flags with replace
.
replace
to replace each occurrence of 'apples' in the string with 'oranges.' <SCRIPT>This prints "oranges are round, and oranges are juicy."
re = /apples/gi;
str = "Apples are round, and apples are juicy.";
newstr=str.replace(re, "oranges");
document.write(newstr)
</SCRIPT>
Example 2. In the following example, the regular expression is defined in replace
and includes the ignore case flag.
<SCRIPT>This prints "Twas the night before Christmas..."
str = "Twas the night before Xmas...";
newstr=str.replace(/xmas/i, "Christmas");
document.write(newstr)
</SCRIPT>
Example 3. The following script switches the words in the string. For the replacement text, the script uses the values of the $1
and $2
properties.
<SCRIPT LANGUAGE="JavaScript1.2">This prints "Smith, John".
re = /(\w+)\s(\w+)/;
str = "John Smith";
newstr = str.replace(re, "$2, $1");
document.write(newstr)
</SCRIPT>
String
object.
Method of |
String
|
Implemented in | Navigator 4.0 |
search(regexp)
regexp | Name of the regular expression. It can be a variable name or a literal. |
search
returns the index of the regular expression inside the string. Otherwise, it returns -1.
When you want to know whether a pattern is found in a string use search
(similar to the regular expression test
method); for more information (but slower execution) use match
(similar to the regular expression exec
method).
function testinput(re, str){
if (str.search(re) != -1)
midstring = " contains ";
else
midstring = " does not contain ";
document.write (str + midstring + re.source);
}
Method of |
String
|
Implemented in | Navigator 2.0, LiveWire 1.0 |
slice(beginslice,endSlice)
beginSlice | The zero-based index at which to begin extraction. |
endSlice |
(Optional) The zero-based index at which to end extraction. If omitted, slice extracts to the end of the string.
|
slice
extracts the text from one string and returns a new string. Changes to the text in one string do not affect the other string.
slice
extracts up to but not including endSlice
. string.slice(1,4)
extracts the second character through the fourth character (characters indexed 1, 2, and 3).
slice
to create a new string. <SCRIPT>This writes:
str1="The morning is upon us. "
str2=str1.slice(3,-5)
document.write(str2)
</SCRIPT>
SMALL
tag.
Method of |
String
|
Implemented in | Navigator 2.0, LiveWire 1.0 |
small()
small
method with the write
or writeln
methods to format and display a string in a document. In server-side JavaScript, use the write
function to display the string.string
methods to change the size of a string:var worldString="Hello, world"
document.write(worldString.small())The previous example produces the same output as the following HTML:
document.write("<P>" + worldString.big())
document.write("<P>" + worldString.fontsize(7))
<SMALL>Hello, world</SMALL>
<P><BIG>Hello, world</BIG>
<P><FONTSIZE=7>Hello, world</FONTSIZE>
String.big
, String.fontsize
String
object into an array of strings by separating the string into substrings.
Method of |
String
|
Implemented in | Navigator 3.0, LiveWire 1.0 |
split(separator, limit)
split
method returns the new array.
When found, separator
is removed from the string and the substrings are returned in an array. If separator
is omitted, the array contains one element consisting of the entire string.
In Navigator 4.0, Split
has the following additions:
separator
is a regular expression, any included parenthesis cause submatches to be included in the returned array. LANGUAGE="JavaScript1.2"
in the SCRIPT
tag, string.split(" ")
splits on any run of 1 or more white space characters including spaces, tabs, line feeds, and carriage returns.function splitString (stringToSplit,separator) {
arrayOfStrings = stringToSplit.split(separator)
document.write ('<P>The original string is: "' + stringToSplit + '"')
document.write ('<BR>The separator is: "' + separator + '"')
document.write ("<BR>The array has " + arrayOfStrings.length + " elements: ")
for (var i=0; i < arrayOfStrings.length; i++) {
document.write (arrayOfStrings[i] + " / ")
}
}
var tempestString="Oh brave new world that has such people in it."
var monthString="Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec"
var space=" "
var comma=","
splitString(tempestString,space)This example produces the following output:
splitString(tempestString)
splitString(monthString,comma)
The original string is: "Oh brave new world that has such people in it."
The separator is: " "
The array has 10 elements: Oh / brave / new / world / that / has / such / people / in / it. /
The original string is: "Oh brave new world that has such people in it."
The separator is: "undefined"
The array has 1 elements: Oh brave new world that has such people in it. /
The original string is: "Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec"Example 2. Consider the following script:
The separator is: ","
The array has 12 elements: Jan / Feb / Mar / Apr / May / Jun / Jul / Aug / Sep / Oct / Nov / Dec /
<SCRIPT LANGUAGE="JavaScript1.2">Using
str="She sells seashells \nby the\n seashore"
document.write(str + "<BR>")
a=str.split(" ")
document.write(a)
</SCRIPT>
LANGUAGE="JavaScript1.2"
, this script produces"She", "sells", "seashells", "by", "the", "seashore"Without
LANGUAGE="JavaScript1.2"
, this script splits only on single space characters, producing "She", "sells", , , , "seashells", "by", , , "the", "seashore"Example 3. In the following example,
split
looks for 0 or more spaces followed by a semicolon followed by 0 or more spaces and, when found, removes the spaces from the string. nameList
is the array returned as a result of split
. <SCRIPT>This prints two lines; the first line prints the original string, and the second line prints the resulting array.
names = "Harry Trump ;Fred Barney; Helen Rigby ; Bill Abel ;Chris Hand ";
document.write (names + "<BR>" + "<BR>");
re = /\s*;\s*/;
nameList = names.split (re);
document.write(nameList);
</SCRIPT>
Harry Trump ;Fred Barney; Helen Rigby ; Bill Abel ;Chris Hand
Harry Trump,Fred Barney,Helen Rigby,Bill Abel,Chris Hand
<SCRIPT LANGUAGE="JavaScript1.2">This script displays the following:
myVar = " Hello World. How are you doing? ";
splits = myVar.split(" ", 3);
document.write(splits)
</SCRIPT>
["Hello", "World.", "How"]
String.charAt
, String.indexOf
, String.lastIndexOf
STRIKE
tag.
Method of |
String
|
Implemented in | Navigator 2.0, LiveWire 1.0 |
strike()
strike
method with the write
or writeln
methods to format and display a string in a document. In server-side JavaScript, use the write
function to display the string.string
methods to change the formatting of a string:var worldString="Hello, world"
document.write(worldString.blink())The previous example produces the same output as the following HTML:
document.write("<P>" + worldString.bold())
document.write("<P>" + worldString.italics())
document.write("<P>" + worldString.strike())
<BLINK>Hello, world</BLINK>
<P><B>Hello, world</B>
<P><I>Hello, world</I>
<P><STRIKE>Hello, world</STRIKE>
String.blink
, String.bold
, String.italics
SUB
tag.
Method of |
String
|
Implemented in | Navigator 2.0, LiveWire 1.0 |
sub()
sub
method with the write
or writeln
methods to format and display a string in a document. In server-side JavaScript, use the write
function to generate the HTML.sub
and sup
methods to format a string:var superText="superscript"
var subText="subscript"
document.write("This is what a " + superText.sup() + " looks like.")The previous example produces the same output as the following HTML:
document.write("<P>This is what a " + subText.sub() + " looks like.")
This is what a <SUP>superscript</SUP> looks like.
<P>This is what a <SUB>subscript</SUB> looks like.
String.sup
Method of |
String
|
Implemented in | Navigator 2.0, LiveWire 1.0 |
substr(start, length)
start | Location at which to begin extracting characters. |
length | (Optional) The number of characters to extract |
start
is a character index. The index of the first character is 0, and the index of the last character is 1 less than the length of the string. substr
begins extracting characters at start
and collects length
number of characters.
If start
is positive and is the length of the string or longer, substr
returns no characters.
<SCRIPT LANGUAGE="JavaScript1.2">
str = "abcdefghij"
document.writeln("(1,2): ", str.substr(1,2))
document.writeln("(-2,2): ", str.substr(-2,2))
document.writeln("(1): ", str.substr(1))
document.writeln("(-20, 2): ", str.substr(1,20))
document.writeln("(20, 2): ", str.substr(20,2))
</SCRIPT>This script displays:
(1,2): bc
(-2,2): ij
(1): bcdefghij
(-20, 2): bcdefghij
(20, 2):
substring
String
object.
Method of |
String
|
Implemented in | Navigator 2.0, LiveWire 1.0 |
substring(indexA, indexB)
indexA | An integer between 0 and 1 less than the length of the string. |
indexB | An integer between 0 and 1 less than the length of the string. |
substring
extracts characters from indexA
up to but not including indexB
. In particular:indexA
is less than 0, indexA
is treated as if it were 0. indexB
is greater than stringName.length
, indexB
is treated as if it were stringName.length
. indexA
equals indexB
, substring
returns an empty string. indexB
is omitted, indexA
extracts characters to the end of the string. LANGUAGE="JavaScript1.2"
in the SCRIPT
tag,
Without LANGUAGE="JavaScript1.2"
,
substring
to display characters from the string "Netscape"
:var anyString="Netscape"
//Displays "Net"Example 2. The following example replaces a substring within a string. It will replace both individual characters and substrings. The function call at the end of the example changes the string
document.write(anyString.substring(0,3))
document.write(anyString.substring(3,0))
//Displays "cap"
document.write(anyString.substring(4,7))
document.write(anyString.substring(7,4))
//Displays "Netscap"
document.write(anyString.substring(0,7))
//Displays "Netscape"
document.write(anyString.substring(0,8))
document.write(anyString.substring(0,10))
"Brave New World"
into "Brave New Web"
.function replaceString(oldS,newS,fullS) {
// Replaces oldS with newS in the string fullS
for (var i=0; i<fullS.length; i++) {
if (fullS.substring(i,i+oldS.length) == oldS) {
fullS = fullS.substring(0,i)+newS+fullS.substring(i+oldS.length,fullS.length)
}
}
return fullS
}
replaceString("World","Web","Brave New World")Example 3. Using
LANGUAGE="JavaScript1.2"
, the following script produces a runtime error (out of memory). <SCRIPT LANGUAGE="JavaScript1.2">Without
str="Netscape"
document.write(str.substring(0,3);
document.write(str.substring(3,0);
</SCRIPT>
LANGUAGE="JavaScript1.2"
, the above script prints
In the second write
, the index numbers are swapped.
substr
SUP
tag.
Method of |
String
|
Implemented in | Navigator 2.0, LiveWire 1.0 |
sup()
sup
method with the write
or writeln
methods to format and display a string in a document. In server-side JavaScript, use the write
function to generate the HTML.sub
and sup
methods to format a string:var superText="superscript"
var subText="subscript"
document.write("This is what a " + superText.sup() + " looks like.")The previous example produces the same output as the following HTML:
document.write("<P>This is what a " + subText.sub() + " looks like.")
This is what a <SUP>superscript</SUP> looks like.
<P>This is what a <SUB>subscript</SUB> looks like.
String.sub
Method of |
String
|
Implemented in | Navigator 2.0, LiveWire 1.0 |
toLowerCase()
toLowerCase
method returns the value of the string converted to lowercase. toLowerCase
does not affect the value of the string itself."alphabet"
:var upperText="ALPHABET"
document.write(upperText.toLowerCase())
String.toUpperCase
Method of |
String
|
Implemented in | Navigator 2.0, LiveWire 1.0 |
toUpperCase()
toUpperCase
method returns the value of the string converted to uppercase. toUpperCase
does not affect the value of the string itself."ALPHABET"
:var lowerText="alphabet"
document.write(lowerText.toUpperCase())
String.toLowerCase
Last Updated: 10/31/97 12:30:31