说明:
String 对象用于处理文本(字符串)。
String对象属性(常用):
length属性:返回字符串中的字符数目。
String对象方法:
1、big()
语法:StringObject.big()
返回值:用于把字符串显示为大号字体。
2、blink()
语法:StringObject.blink()
注:此方法无法工作于 Internet Explorer 中。
返回值:用于显示闪动的字符串。
3、bold()
语法:StringObject.bold()
返回值:用于把字符串显示为粗体。
4、charAt()
语法:StringObject.charAt(index)
说明:index表示字符串中某个位置的数字;字符串中第一个字符的下标是 0。如果参数 index 不在 0 与 string.length 之间,该方法将返回一个空字符串。
返回值:返回指定位置的字符
实例:
<html>
<head>
</head>
<body>
<script type="text/javascript">
var txt="hello world!";
//返回在指定位置的字符。
document.write("<p>"+"charAt2:"+txt.charAt(1)+"</p>");
</script>
</body>
</html>
运行结果:charAt2:e
5、charCodeAt()
语法:StringObject.charCodeAt(index)
说明:字符串中第一个字符的下标是 0。如果 index 是负数,或大于等于字符串的长度,则 charCodeAt() 返回 NaN。
返回值:返回指定位置的字符的 Unicode 编码。这个返回值是 0 - 65535 之间的整数。
注:方法 charCodeAt() 与 charAt() 方法执行的操作相似,只不过前者返回的是位于指定位置的字符的编码,而后者返回的是字符子串。
6、concat()
语法:stringObject.concat(string1,string2,...,stringn)
返回值:用于连接两个或多个字符串。
注:使用 " + " 运算符来进行字符串的连接运算通常会更简便一些。
7、fixed()
语法:StringObject.fixed()
返回值:用于把字符串显示为打字机字体。
8、fontcolor()
语法:StringObject.fontcolor(color)
说明:color必须是颜色名(red)、RGB 值(rgb(255,0,0))或者十六进制数(#FF0000)。
返回值:用于按照指定的颜色来显示字符串。
9、fontsize()
语法:stringObject.fontsize(size)
注:size 参数必须是从 1 至 7 的数字。
返回值:用于按照指定的尺寸来显示字符串。
10、fromCharCode()
语法:String.fromCharCode(num1,num2,num3,...,numn)
说明:a、numn为Unicode值
b、它的语法应该是 String.fromCharCode(),而不是 myStringObject.fromCharCode()。
返回值: 可接受一个指定的 Unicode 值,然后返回一个字符串。
11、indexOf()
语法:stringObject.indexOf(searchvalue,fromindex)
说明:searchvalue要检索的字符串;fromindex可选,规定开始检索的位置
返回值:返回某个指定的字符串值在字符串中首次出现的位置。
实例:
<html>
<head>
</head>
<body>
<script type="text/javascript">
var txt="hello world!";
//indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置。
document.write("<p>"+"hello的起始位置:"+txt.indexOf("hello")+"</p>");
document.write("<p>"+"World的起始位置:"+txt.indexOf("World")+"</p>");
document.write("<p>"+"world的起始位置:"+txt.indexOf("world")+"</p>");
</script>
</body>
</html>
运行结果:
hello的起始位置:0
World的起始位置:-1
world的起始位置:6
12、lastIndexOf()
语法:stringObject.lastIndexOf(searchvalue,fromindex)
说明:该方法将从尾到头地检索字符串 stringObject,看它是否含有子串 searchvalue。开始检索的位置在字符串的 fromindex 处或字符串的结尾(没有指定 fromindex 时)。如果找到一个 searchvalue,则返回 searchvalue 的第一个字符在 stringObject 中的位置。stringObject 中的字符位置是从 0 开始的。
返回值:返回一个指定的字符串值最后出现的位置,在一个字符串中的指定位置从后向前搜索。
实例:
<html>
<head>
</head>
<body>
<script type="text/javascript">
var txt="hello world!";
//lastIndexOf() 方法可返回一个指定的字符串值最后出现的位置,在一个字符串中的指定位置从后向前搜索。
document.write(txt.lastIndexOf("Hello") + "<br />")
document.write(txt.lastIndexOf("World") + "<br />")
document.write(txt.lastIndexOf("world"))
</script>
</body>
</html>
运行结果:-1
-1
6
剩余实例及其运行结果
<html>
<head>
</head>
<body>
<script type="text/javascript">
var txt="hello world!";
//字符串的长度
document.write("<p>"+"长度:"+txt.length+"</p>");
//用大号字体显示字符串。
document.write("<p>"+"字体变大:"+txt.big()+"</p>");
//显示闪动字符串
document.write("<p>"+"闪烁:"+txt.blink()+"</p>");
//使用粗体显示字符串。
document.write("<p>"+"粗体:"+txt.bold()+"</p>");
//返回在指定位置的字符。
document.write("<p>"+"charAt2:"+txt.charAt(1)+"</p>");
//返回在指定的位置的字符的 Unicode 编码。
document.write("<p>"+"charCodeAt2:"+txt.charCodeAt(2)+"</p>");
//连接字符串。
var txt1="world"
document.write("<p>"+"concat连接:"+txt.concat(txt1)+"</p>");
//以打字机文本显示字符串。
document.write("<p>"+"以打字机文本显示字符串:"+txt.fixed()+"</p>");
//使用指定的颜色来显示字符串。
document.write("<p>"+"颜色:"+txt.fontcolor("blue")+"</p>");
//使用指定的尺寸来显示字符串。
document.write("<p>"+"大小:"+txt.fontsize(7)+"</p>");
//从字符编码创建一个字符串。
document.write("从字符编码创建一个字符串:"+"<br />")
document.write(String.fromCharCode(72,69,76,76,79))
document.write("<br />")
document.write(String.fromCharCode(65,66,67))
</script>
</body>
</html>
运行结果