教程·Javascript 中文手册
An object representing a series of characters in a string.
| Core object | |
| 实现版本 | Navigator 2.0: Create a String object only by quoting characters. Navigator 3.0, LiveWire 1.0: 添加了 String constructor; 添加了 prototype 属性; 添加了 | substr方法s.
The String constructor:
new String(string);
| string | Any string. |
The 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'.
| Allows the addition of properties to a String object. |
| Returns the calling string value converted to uppercase. |
示例 1: String variable.The following statement creates a string variable:
var last_name = "Schaefer"示例 2: String object properties.The following statements evaluate to 8, "SCHAEFER," and "schaefer":
last_name.length
last_name.toUpperCase()
last_name.toLowerCase()示例 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:
var myString = "Hello"
document.write ("The first character in the string is " + myString[0])displays "The first character in the string is H"
示例 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")
var firstName = new String ("Jesse")
empWindow=window.open('string2.html','window1','width=300,height=300')If the HTML source for the second window (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=firstName
empWindow.empLastName=lastNameThe following code in the first window displays the values of the second window's variables:
alert('empFirstName in empWindow is ' + empWindow.empFirstName)
alert('empLastName in empWindow is ' + empWindow.empLastName)
The length of the string.
| 属性源 | Navigator 2.0, LiveWire 1.0 |
For a null string, length is 0.
The following example displays 8 in an Alert dialog box:
var x="Netscape"
alert("The string length is " + x.length)
Represents the prototype for this class. You can use the prototype to add properties or methods to all instances of a class. For information on prototypes, seeFunction.prototype.
| 属性源 | Navigator 3.0, Netscape Server 3.0 |
Creates an HTML anchor that is used as a hypertext target.
| 方法源 | Navigator 2.0, LiveWire 1.0 |
anchor(nameAttribute)
| nameAttribute | A string. |
Use the anchor method with thewritefunction to display the anchor.
In the语法, the text string represents the literal text that you want the user to see. The nameAttribute string represents the NAME attribute of the A tag.
Anchors created with the anchor method become elements in thedocument.anchorsarray.
The following example opens the msgWindow window and creates an anchor for the 表 of contents:
var myString="表 of Contents"
msgWindow.document.writeln(myString.anchor("contents_anchor"))The previous example produces the same output as the following HTML:
<A NAME="contents_anchor">表 of Contents</A>In server-side JavaScript, you can generate this HTML by calling thedocument.writeln
Causes a string to be displayed in a big font as if it were in aBIGtag. big() 无 Use the big method with thewritefunction to display the string.
The following example uses string methods to change the size of a string: var worldString="Hello, world"参看
String.link
big
方法源
Navigator 2.0, LiveWire 1.0
语法
参数
描述
示例
document.write("<P>" + worldString.big())
document.write("<P>" + worldString.fontsize(7))The previous example produces the same output as the following HTML:
<SMALL>Hello, world</SMALL>
<P><BIG>Hello, world</BIG>
<P><FONTSIZE=7>Hello, world</FONTSIZE>
Causes a string to blink as if it were in aBLINKtag.
| 方法源 | Navigator 2.0, LiveWire 1.0 |
blink()
无
Use the blink method with thewritefunction to display the string.
The following example uses string methods to change the formatting of a string:
var worldString="Hello, world"document.write(worldString.blink())
document.write("<P>" + worldString.bold())
document.write("<P>" + worldString.italics())
document.write("<P>" + worldString.strike())The previous example produces the same output as the following HTML:
<BLINK>Hello, world</BLINK>
<P><B>Hello, world</B>
<P><I>Hello, world</I>
<P><STRIKE>Hello, world</STRIKE>
Causes a string to be displayed as bold as if it were in aBtag.
| 方法源 | Navigator 2.0, LiveWire 1.0 |
bold()
无
Use the bold method with thewritefunction to display the string.
The following example uses string methods to change the formatting of a string:
var worldString="Hello, world"
document.write(worldString.blink())
document.write("<P>" + worldString.bold())
document.write("<P>" + worldString.italics())
document.write("<P>" + worldString.strike())The previous example produces the same output as the following HTML:
<BLINK>Hello, world</BLINK>
<P><B>Hello, world</B>
<P><I>Hello, world</I>
<P><STRIKE>Hello, world</STRIKE>
Returns the specified character from the string.
| 方法源 | Navigator 2.0, LiveWire 1.0 |
charAt(index)
| index | An integer between 0 and 1 less than the length of the string. |
Characters in a string are indexed from left to right. The index of the first character is 0, and the index of the last character in a string called stringName is stringName.length - 1. If the index you supply is out of range, JavaScript returns an empty string.
The following example displays characters at different locations in the string "Brave new world":
var anyString="Brave new world"document.writeln("The character at index 0 is " + anyString.charAt(0))
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))These lines display the following:
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 thedocument.write
Returns a number indicating the ISO-Latin-1 codeset value of the character at the given index.
| 方法源 | 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. |
The ISO-Latin-1 codeset ranges from 0 to 255. The first 0 to 127 are a direct match of the ASCII character set.
The following example returns 65, the ISO-Latin-1 codeset value for A.
"ABC".charCodeAt(0)
Combines the text of two strings and returns a new string.
| 方法源 | 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.
The following example combines two strings into a new string.
<SCRIPT>
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>This writes:
The morning is upon us.
The sun is bright.
The morning is upon us. The sun is bright.
Causes a string to be displayed in fixed-pitch font as if it were in aTTtag.
| 方法源 | Navigator 2.0, LiveWire 1.0 |
fixed()
无
Use the fixed method with thewritefunction to display the string.
The following example uses the fixed method to change the formatting of a string:
var worldString="Hello, world"
document.write(worldString.fixed())The previous example produces the same output as the following HTML:
<TT>Hello, world</TT>
Causes a string to be displayed in the specified color as if it were in a <FONTCOLOR=color> tag.
| 方法源 | 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 | JavaScript Guide |
Use the fontcolor method with thewritefunction to display the string.
If you express color as a hexadecimal RGB triplet, you must use the format rrggbb. For example, the hexadecimal RGB values for salmon are red=FA, green=80, and blue=72, so the RGB triplet for salmon is "FA8072".
The fontcolor method overrides a value set in the fgColor property.
The following example uses the 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") +
" 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")The previous example produces the same output as the following HTML:
<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
Causes a string to be displayed in the specified font size as if it were in a <FONTSIZE=size> tag.
| 方法源 | 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. |
Use the fontsize method with thewritefunction to display the string.
When you specify size as an integer, you set the size of stringName to one of the 7 defined sizes. When you specify size as a string such as "-2", you adjust the font size of stringName relative to the size set in the BASEFONT tag.
The following example uses string methods to change the size of a string:
var worldString="Hello, world"document.write(worldString.small())
document.write("<P>" + worldString.big())
document.write("<P>" + worldString.fontsize(7))The previous example produces the same output as the following HTML:
<SMALL>Hello, world</SMALL>
<P><BIG>Hello, world</BIG>
<P><FONTSIZE=7>Hello, world</FONTSIZE>
Returns a string created by using the specified sequence ISO-Latin-1 codeset values.
| 方法源 | Navigator 4.0, Netscape Server 3.0 |
fromCharCode(num1, ..., numN)
| num1, ..., numN | A sequence of numbers that are ISO-Latin-1 codeset values. |
This method returns a string and not a 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.
示例 1. The following example returns the string "ABC".
String.fromCharCode(65,66,67)示例 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)
Returns the index within the calling String object of the first occurrence of the specified value, starting the search at fromIndex, or -1 if the value is not found.
| 方法源 | Navigator 2.0, LiveWire 1.0 |
indexOf(searchValue, fromIndex)
| searchValue | A string representing the value to search for. |
| fromIndex | (Optional) The location within the calling string to start the search from. It can be any integer between 0 and 1 less than the length of the string. The default value is 0. |
Characters in a string are indexed from left to right. The index of the first character is 0, and the index of the last character of a string called 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")
示例 1.The following example uses indexOf and lastIndexOf to locate values in the string "Brave new world."
var anyString="Brave new world"//Displays 8
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"))示例 2.The following example defines two string variables. The variables contain the same string except that the second string contains uppercase letters. The firstwritelnmethod displays -1.
myString="brie, pepper jack, cheddar"
myCapString="Brie, Pepper Jack, Cheddar"
document.writeln('myString.indexOf("cheddar") is ' +
myString.indexOf("cheddar"))
document.writeln('<P>myCapString.indexOf("cheddar") is ' +
myCapString.indexOf("cheddar"))示例 3.The following example sets 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);
}
Causes a string to be italic, as if it were in an italics() 无 Use the italics method with thewritefunction to display the string.
The following example uses string methods to change the formatting of a string: var worldString="Hello, world"document.write(worldString.blink()) <BLINK>Hello, world</BLINK> Returns the index within the calling String object of the last occurrence of the specified value. The calling string is searched backward, starting at fromIndex, or -1 if not found. lastIndexOf(searchValue, fromIndex) Characters in a string are indexed from left to right. The index of the first character is 0, and the index of the last character is stringName.length - 1. The lastIndexOf method is case sensitive. For example, the following expression returns -1: "Blue Whale, Killer Whale".lastIndexOf("blue") The following example uses indexOf and lastIndexOf to locate values in the string "Brave new world." var anyString="Brave new world"//Displays 8 Creates an HTML hypertext link that requests another URL. link(hrefAttribute) Use the link method to programmatically create a hypertext link, and then callwritefunction to display the link. Links created with the link method become elements in the links array of the document.links
The following example displays the word "Netscape" as a hypertext link that returns the user to the Netscape home page: var hotText="Netscape" Click to return to <A HREF="http://home.netscape.com">Netscape</A> Used to match a regular expression against a string. match(regexp) If you want to execute a global match, or a case insensitive match, 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示例 below show how to use these flags with match. If you execute a match simply to find true or false, useString.searchor the regular expression test method.
示例 1. In the following example, 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> '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). 示例 2. The following example demonstrates the use of the global and ignore case flags with match. <SCRIPT> Used to find a match between a regular expression and a string, and to replace the matched substring with a new substring. replace(regexp, newSubStr) This method does not change the 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示例 below show how to use these flags with replace. 示例 1. In the following example, the regular expression includes the global and ignore case flags which permits replace to replace each occurrence of 'apples' in the string with 'oranges.' <SCRIPT> 示例 2. In the following example, the regular expression is defined in replace and includes the ignore case flag. <SCRIPT> 示例 3. The following script switches the words in the string. For the replacement text, the script uses the values of the $1 and properties. <SCRIPT LANGUAGE="JavaScript1.2"> Executes the search for a match between a regular expression and this String object. search(regexp) If successful, 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) usematch(similar to the regular expression exec method).
The following example prints a message which depends on the success of the test. function testinput(re, str){ Extracts a section of a string and returns a new string. slice(beginslice,endSlice) 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). As a negative index, endSlice indicates an offset from the end of the string. string.slice(2,-1) extracts the third character through the second to last character in the string. The following example uses slice to create a new string. <SCRIPT> morning is upon Causes a string to be displayed in a small font, as if it were in a small() 无 Use the small method with thewritefunction to display the string.
The following example uses string methods to change the size of a string: var worldString="Hello, world"document.write(worldString.small()) <SMALL>Hello, world</SMALL> Splits a String object into an array of strings by separating the string into substrings. split(separator, limit) The 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: 示例 1. The following example defines a function that splits a string into an array of strings using the specified separator. After splitting the string, the function displays messages indicating the original string (before the split), the separator used, the number of elements in the array, and the individual array elements. function splitString (stringToSplit,separator) { The original string is: "Oh brave new world that has such people in it." <SCRIPT LANGUAGE="JavaScript1.2"> "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"示例 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> Harry Trump ;Fred Barney; Helen Rigby ; Bill Abel ;Chris Hand 示例 4. In the following example, split looks for 0 or more spaces in a string and returns the first 3 splits that it finds. <SCRIPT LANGUAGE="JavaScript1.2"> ["Hello", "World.", "How"] Causes a string to be displayed as struck-out text, as if it were in aSTRIKEtag. strike() 无 Use the strike method with thewritefunction to display the string.
The following example uses string methods to change the formatting of a string: var worldString="Hello, world"document.write(worldString.blink()) <BLINK>Hello, world</BLINK> Causes a string to be displayed as a subscript, as if it were in aSUBtag. sub() 无 Use the sub method with thewritefunction to generate the HTML.
The following example uses the sub and sup methods to format a string: var superText="superscript" This is what a <SUP>superscript</SUP> looks like. Returns the characters in a string beginning at the specified location through the specified number of characters. substr(start, length) 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. If start is negative, substr uses it as a character index from the end of the string. If start is negative and abs(start) is larger than the length of the string, substr uses 0 is the start index. If length is 0 or negative, substr returns no characters. If length is omitted, start extracts characters to the end of the string. Consider the following script: <SCRIPT LANGUAGE="JavaScript1.2">str = "abcdefghij" (1,2): bc Returns a subset of a String object. substring(indexA, indexB) substring extracts characters from indexA up to but not including indexB. In particular: Using LANGUAGE="JavaScript1.2" in the SCRIPT tag, Without LANGUAGE="JavaScript1.2", 示例 1.The following example uses substring to display characters from the string "Netscape": var anyString="Netscape"//Displays "Net" function replaceString(oldS,newS,fullS) { <SCRIPT LANGUAGE="JavaScript1.2"> Net Net In the second write, the index numbers are swapped. Causes a string to be displayed as a superscript, as if it were in aSUPtag. sup() 无 Use the sup method with thewritefunction to generate the HTML.
The following example uses the sub and sup methods to format a string: var superText="superscript" This is what a <SUP>superscript</SUP> looks like. Returns the calling string value converted to lowercase. toLowerCase() 无 The toLowerCase method returns the value of the string converted to lowercase. toLowerCase does not affect the value of the string itself. The following example displays the lowercase string "alphabet": var upperText="ALPHABET" Returns the calling string value converted to uppercase. toUpperCase() 无 The toUpperCase method returns the value of the string converted to uppercase. toUpperCase does not affect the value of the string itself. The following example displays the string "ALPHABET": var lowerText="alphabet"Itag.
方法源
Navigator 2.0, LiveWire 1.0
语法
参数
描述
示例
document.write("<P>" + worldString.bold())
document.write("<P>" + worldString.italics())
document.write("<P>" + worldString.strike())The previous example produces the same output as the following HTML:
<P><B>Hello, world</B>
<P><I>Hello, world</I>
<P><STRIKE>Hello, world</STRIKE>参看
String.strike
lastIndexOf
方法源
Navigator 2.0, LiveWire 1.0
语法
参数
searchValue
A string representing the value to search for.
fromIndex
(Optional) The location within the calling string to start the search from. It can be any integer between 0 and 1 less than the length of the string. The default value is 1 less than the length of the string.
描述
示例
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"))In server-side JavaScript, you can display the same output by calling thedocument.write
参看
String.split
link
方法源
Navigator 2.0, LiveWire 1.0
语法
参数
hrefAttribute
Any string that specifies the HREF attribute of the A tag; it should be a valid URL (relative or absolute).
描述
示例
document.write("Click to return to " + hotText.link(URL))The previous example produces the same output as the following HTML:参看
Anchor
match
方法源
Navigator 4.0
语法
参数
regexp
Name of the regular expression. It can be a variable name or a literal.
描述
Note
示例
str = "For more information, see Chapter 3.4.5.1";
re = /(chapter \d+(\.\d)*)/i;
found = str.match(re);
document.write(found);
</SCRIPT>This returns the array containing Chapter 3.4.5.1,Chapter 3.4.5.1,.1
str = "abcDdcba";
newArray = str.match(/d/gi);
document.write(newArray);
</SCRIPT>The returned array contains D, d. replace
方法源
Navigator 4.0
语法
参数
regexp
The name of the regular expression. It can be a variable name or a literal.
newSubStr
The string to put in place of the string found with regexp. This string can include the rightContext.
描述
示例
re = /apples/gi;
str = "Apples are round, and apples are juicy.";
newstr=str.replace(re, "oranges");
document.write(newstr)
</SCRIPT>This prints "oranges are round, and oranges are juicy."
str = "Twas the night before Xmas...";
newstr=str.replace(/xmas/i, "Christmas");
document.write(newstr)
</SCRIPT>This prints "Twas the night before Christmas..."
re = /(\w+)\s(\w+)/;
str = "John Smith";
newstr = str.replace(re, ", $1");
document.write(newstr)
</SCRIPT>This prints "Smith, John". search
方法源
Navigator 4.0
语法
参数
regexp
Name of the regular expression. It can be a variable name or a literal.
描述
示例
if (str.search(re) != -1)
midstring = " contains ";
else
midstring = " does not contain ";
document.write (str + midstring + re.source);
}
slice
方法源
Navigator 2.0, LiveWire 1.0
语法
参数
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.
描述
示例
str1="The morning is upon us. "
str2=str1.slice(3,-5)
document.write(str2)
</SCRIPT>small
SMALLtag.
方法源
Navigator 2.0, LiveWire 1.0
语法
参数
描述
示例
document.write("<P>" + worldString.big())
document.write("<P>" + worldString.fontsize(7))The previous example produces the same output as the following HTML:
<P><BIG>Hello, world</BIG>
<P><FONTSIZE=7>Hello, world</FONTSIZE>参看
String.fontsize
split
方法源
Navigator 3.0, LiveWire 1.0
语法
参数
separator
(Optional) Specifies the character to use for separating the string. The separator is treated as a string. If separator is omitted, the array returned contains one element consisting of the entire string.
limit
(Optional) Integer specifying a limit on the number of splits to be found.
描述
示例
splitString(tempestString,space)
splitString(tempestString)
splitString(monthString,comma)This example produces the following output:
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"
The separator is: ","
The array has 12 elements: Jan / Feb / Mar / Apr / May / Jun / Jul / Aug / Sep / Oct / Nov / Dec /示例 2. Consider the following script:
str="She sells seashells
by the
seashore"
document.write(str + "<BR>")
a=str.split(" ")
document.write(a)
</SCRIPT>Using LANGUAGE="JavaScript1.2", this script produces
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>This prints two lines; the first line prints the original string, and the second line prints the resulting array.
Harry Trump,Fred Barney,Helen Rigby,Bill Abel,Chris Hand
myVar = " Hello World. How are you doing? ";
splits = myVar.split(" ", 3);
document.write(splits)
</SCRIPT>This script displays the following:参看
String.lastIndexOf
strike
方法源
Navigator 2.0, LiveWire 1.0
语法
参数
描述
示例
document.write("<P>" + worldString.bold())
document.write("<P>" + worldString.italics())
document.write("<P>" + worldString.strike())The previous example produces the same output as the following HTML:
<P><B>Hello, world</B>
<P><I>Hello, world</I>
<P><STRIKE>Hello, world</STRIKE>参看
String.italics
sub
方法源
Navigator 2.0, LiveWire 1.0
语法
参数
描述
示例
document.write("This is what a " + superText.sup() + " looks like.")
document.write("<P>This is what a " + subText.sub() + " looks like.")The previous example produces the same output as the following HTML:
<P>This is what a <SUB>subscript</SUB> looks like.参看
String.sup
substr
方法源
Navigator 2.0, LiveWire 1.0
语法
参数
start
Location at which to begin extracting characters.
length
(Optional) The number of characters to extract
描述
示例
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:
(-2,2): ij
(1): bcdefghij
(-20, 2): bcdefghij
(20, 2): 参看
substring
substring
方法源
Navigator 2.0, LiveWire 1.0
语法
参数
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.
描述
示例
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))示例 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 "Brave New World" into "Brave New Web".
// 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")示例 3. Using LANGUAGE="JavaScript1.2", the following script produces a runtime error (out of memory).
str="Netscape"
document.write(str.substring(0,3);
document.write(str.substring(3,0);
</SCRIPT>Without LANGUAGE="JavaScript1.2", the above script prints 参看
substr
sup
方法源
Navigator 2.0, LiveWire 1.0
语法
参数
描述
示例
document.write("This is what a " + superText.sup() + " looks like.")
document.write("<P>This is what a " + subText.sub() + " looks like.")The previous example produces the same output as the following HTML:
<P>This is what a <SUB>subscript</SUB> looks like.参看
String.sub
toLowerCase
方法源
Navigator 2.0, LiveWire 1.0
语法
参数
描述
示例
document.write(upperText.toLowerCase())参看
String.toUpperCase
toUpperCase
方法源
Navigator 2.0, LiveWire 1.0
语法
参数
描述
示例
document.write(lowerText.toUpperCase())参看
String.toLowerCase