HTML DOM教程 50-JavaScript String 对象
字符串是 JavaScript 的一种基本的数据类型。
String 对象的 length 属性声明了该字符串中的字符数。String 类定义了大量操作字符串的方法。
需要注意的是,JavaScript 的字符串是不可变的,String 类定义的方法都不能改变字符串的内容。
1:String 对象的方法
方法 | 描述 | FF | N | IE |
---|---|---|---|---|
anchor() | 创建 HTML 锚。 | 1 | 2 | 3 |
big() | 用大号字体显示字符串。 | 1 | 2 | 3 |
blink() | 显示闪动字符串。 | 1 | 2 | |
bold() | 使用粗体显示字符串。 | 1 | 2 | 3 |
charAt() | 返回在指定位置的字符。 | 1 | 2 | 3 |
charCodeAt() | 返回在指定的位置的字符的 Unicode 编码。 | 1 | 4 | 4 |
concat() | 连接字符串。 | 1 | 4 | 4 |
fixed() | 以打字机文本显示字符串。 | 1 | 2 | 3 |
fontcolor() | 使用指定的颜色来显示字符串。 | 1 | 2 | 3 |
fontsize() | 使用指定的尺寸来显示字符串。 | 1 | 2 | 3 |
fromCharCode() | 从字符编码创建一个字符串。 | 1 | 4 | 4 |
indexOf() | 检索字符串。 | 1 | 2 | 3 |
italics() | 使用斜体显示字符串。 | 1 | 2 | 3 |
lastIndexOf() | 从后向前搜索字符串。 | 1 | 2 | 3 |
link() | 将字符串显示为链接。 | 1 | 2 | 3 |
localeCompare() | 用本地特定的顺序来比较两个字符串。 | 1 | 4 | 4 |
match() | 找到一个或多个正在表达式的匹配。 | 1 | 4 | 4 |
replace() | 替换与正则表达式匹配的子串。 | 1 | 4 | 4 |
search() | 检索与正则表达式相匹配的值。 | 1 | 4 | 4 |
slice() | 提取字符串的片断,并在新的字符串中返回被提取的部分。 | 1 | 4 | 4 |
small() | 使用小字号来显示字符串。 | 1 | 2 | 3 |
split() | 把字符串分割为字符串数组。 | 1 | 4 | 4 |
strike() | 使用删除线来显示字符串。 | 1 | 2 | 3 |
sub() | 把字符串显示为下标。 | 1 | 2 | 3 |
substr() | 从起始索引号提取字符串中指定数目的字符。 | 1 | 4 | 4 |
substring() | 提取字符串中两个指定的索引号之间的字符。 | 1 | 2 | 3 |
sup() | 把字符串显示为上标。 | 1 | 2 | 3 |
toLocaleLowerCase() | 把字符串转换为小写。 | - | - | - |
toLocaleUpperCase() | 把字符串转换为大写。 | - | - | - |
toLowerCase() | 把字符串转换为小写。 | 1 | 2 | 3 |
toUpperCase() | 把字符串转换为大写。 | 1 | 2 | 3 |
toSource() | 代表对象的源代码。 | 1 | 4 | - |
toString() | 返回字符串。 | - | - | - |
valueOf() | 返回某个字符串对象的原始值。 | 1 | 2 | 4 |
2:String 对象的属性
属性 | 描述 | FF | N | IE |
---|---|---|---|---|
constructor | 对创建该对象的函数的引用 | 1 | 4 | 4 |
length | 字符串的长度 | 1 | 2 | 3 |
prototype | 允许您向对象添加属性和方法 | 1 | 2 | 4 |
3:anchor() 方法
在本例中,我们会为文本添加一个锚:
<script type="text/javascript">
var txt="Hello world!"
document.write(txt.anchor("myanchor"))
</script>上面的代码将输出为纯粹的 HTML:
<a name="myanchor">Hello world!</a>
4:blink(),big(),sub(),fixed()等方法
<html>
<body>
<script type="text/javascript">
var txt="Hello World!"
document.write("<p>Big: " + txt.big() + "</p>")
document.write("<p>Small: " + txt.small() + "</p>")
document.write("<p>Bold: " + txt.bold() + "</p>")
document.write("<p>Italic: " + txt.italics() + "</p>")
document.write("<p>Blink: " + txt.blink() + " (does not work in IE)</p>")
document.write("<p>Fixed: " + txt.fixed() + "</p>")
document.write("<p>Strike: " + txt.strike() + "</p>")
document.write("<p>Fontcolor: " + txt.fontcolor("Red") + "</p>")
document.write("<p>Fontsize: " + txt.fontsize(16) + "</p>")
document.write("<p>Lowercase: " + txt.toLowerCase() + "</p>")
document.write("<p>Uppercase: " + txt.toUpperCase() + "</p>")
document.write("<p>Subscript: " + txt.sub() + "</p>")
document.write("<p>Superscript: " + txt.sup() + "</p>")
document.write("<p>Link: " + txt.link("http://www.w3school.com.cn") + "</p>")
</script>
</body>
</html>
5:concat() 方法
在本例中,我们将创建两个字符串,然后使用 concat() 把它们显示为一个字符串:
<script type="text/javascript">
var str1="Hello "
var str2="world!"
document.write(str1.concat(str2))
</script>以上代码的输出是:
Hello world!6:indexOf() 方法
注释1:indexOf() 方法对大小写敏感!
注释2:如果要检索的字符串值没有出现,则该方法返回 -1。
在本例中,我们将在 "Hello world!" 字符串内进行不同的检索:
<script type="text/javascript">
var str="Hello world!"
document.write(str.indexOf("Hello") + "<br />")
document.write(str.indexOf("World") + "<br />")
document.write(str.indexOf("world"))
</script>以上代码的输出:
0
-1
67:substring() 方法
语法:stringObject.substring(start,stop):如果省略stop参数,那么返回的子串会一直到字符串的结尾;如果参数 start 与 end 相等,那么该方法返回的就是一个空串(即长度为 0 的字符串)。如果 start 比 end 大,那么该方法在提取子串之前会先交换这两个参数。
重要事项:与 slice() 和 substr() 方法不同的是,substring() 不接受负的参数。
在本例中,我们将使用 substring() 从字符串中提取一些字符:
<script type="text/javascript">
var str="Hello world!"
document.write(str.substring(3))
</script>输出:
lo world!8:split() 方法
语法:stringObject.split(separator,howmany):参数howmany可指定返回的数组的最大长度。如果设置了该参数,返回的子串不会多于这个参数指定的数组。如果没有设置该参数,整个字符串都会被分割,不考虑它的长度。
注释:如果把空字符串 ("") 用作 separator,那么 stringObject 中的每个字符之间都会被分割。
例子 1
在本例中,我们将按照不同的方式来分割字符串:
<script type="text/javascript">
var str="How are you doing today?"
document.write(str.split(" ") + "<br />")
document.write(str.split("") + "<br />")
document.write(str.split(" ",3))
</script>输出:
How,are,you,doing,today?
H,o,w, ,a,r,e, ,y,o,u, ,d,o,i,n,g, ,t,o,d,a,y,?
How,are,you例子 2
在本例中,我们将分割结构更为复杂的字符串:
"2:3:4:5".split(":") //将返回["2", "3", "4", "5"]
"|a|b|c".split("|") //将返回["", "a", "b", "c"]
例子 3
使用下面的代码,可以把句子分割成单词:
var words = sentence.split(' ')或者使用正则表达式作为 separator:
var words = sentence.split(/"s+/)例子 4
如果您希望把单词分割为字母,或者把字符串分割为字符,可使用下面的代码:
"hello".split("") //可返回 ["h", "e", "l", "l", "o"]若只需要返回一部分字符,请使用 howmany 参数:
"hello".split("", 3) //可返回 ["h", "e", "l"]9:match() 方法
match() 方法可在字符串内检索指定的值,或找到一个或多个正则表达式的匹配。该方法类似 indexOf() 和 lastIndexOf(),但是它返回指定的值,而不是字符串的位置。
语法:stringObject.match(searchvalue),其中 searchvalue必需。规定要检索的字符串值。
或者stringObject.match(regexp),其中regexp必需。规定要匹配的模式的 RegExp 对象。如果该参数不是 RegExp 对象,则需要首先把它传递给 RegExp 构造函数,将其转换为 RegExp 对象。
match() 方法将检索字符串 stringObject,以找到一个或多个与 regexp 匹配的文本。这个方法的行为在很大程度上有赖于 regexp 是否具有标志 g。
如果 regexp 没有标志 g,那么 match() 方法就只能在 stringObject 中执行一次匹配。如果没有找到任何匹配的文本, match() 将返回 null。否则,它将返回一个数组,其中存放了与它找到的匹配文本有关的信息。该数组的第 0 个元素存放的是匹配文本,而其余的元素存放的是与正则表达式的子表达式匹配的文本。除了这些常规的数组元素之外,返回的数组还含有两个对象属性。index 属性声明的是匹配文本的起始字符在 stringObject 中的位置,input 属性声明的是对 stringObject 的引用。
如果 regexp 具有标志 g,则 match() 方法将执行全局检索,找到 stringObject 中的所有匹配子字符串。若没有找到任何匹配的子串,则返回 null。如果找到了一个或多个匹配子串,则返回一个数组。不过全局匹配返回的数组的内容与前者大不相同,它的数组元素中存放的是 stringObject 中所有的匹配子串,而且也没有 index 属性或 input 属性。
注意:在全局检索模式下,match() 即不提供与子表达式匹配的文本的信息,也不声明每个匹配子串的位置。如果您需要这些全局检索的信息,可以使用 RegExp.exec()。
例子 1
在本例中,我们将在 "Hello world!" 中进行不同的检索:
<script type="text/javascript">
var str="Hello world!"
document.write(str.match("world") + "<br />")
document.write(str.match("World") + "<br />")
document.write(str.match("worlld") + "<br />")
document.write(str.match("world!"))
</script>输出:
world
null
null
world!例子 2
在本例中,我们将使用全局匹配的正则表达式来检索字符串中的所有数字:
<script type="text/javascript">
var str="1 plus 2 equal 3"
document.write(str.match(/"d+/g)
)
</script>
输出:
1,2,3