JavaScript中String的属性和方法

String只有一个属性,就是length,length表示的是字符的数目,而不是编码长度,无论页面是gb2312编码还是utf-8编码,汉字都被认为是一个长度。

<script>
var a = "我爱我家";
var b = "I love home";
document.write(a.length);
document.write(b.length);
//显示:4 11(加两个空格)
</script>
String的方法比较多,从简单到复杂,一个一个讲解:

toLowerCase()方法可以把字符串转换成小写,toUpperCase()方法正好相仿,把字符串转换 成大写,使用方法也非常简单。 toLocaleUpperCase()和toLocaleLowerCase()方法是为了支持少数有Unicode字符冲突的语言(比如土耳其语), 没有特殊情况,完全可以放弃使用:

<script>
var c = "AAA";
var d = "aaa";
document.write(c.toLowerCase());
document.write(d.toUpperCase());
document.write(c.toLocaleLowerCase());
document.write(d.toLocaleUpperCase());
//显示:aaa AAA aaa AAA
</script>
localeCompare()方法可以用作比较字符串,使用方法如下:

<script>
var e = "我";
var f = e.localeCompare("我");
var g = e.localeCompare("你");
document.write(f);
document.write(g);
//显示:0 1
</script>
toString()方法将对象转换成字符串,valueOf()方法获取字符串的值,在一些JavaScript的参考书对两者的解释是相同的,但是仔细研究下面的例子,还是有微小的差别的,很白痴的两个方法,不多讲:

<script>
var h = 1;
document.write(typeof(h));
document.write(typeof(h.toString()));
document.write(typeof(h.valueOf()));
//显示:number string number
var i = "1";
document.write(typeof(i));
document.write(typeof(i.toString()));
document.write(typeof(i.valueOf()));
//显示:string string string
</script>
concat()方法可以拼合任意多个字符串,concat()方法不怎么被提及的原因是因为有运算符“+”,既生瑜何生亮!好在堂姐Array那边还有一个concat方法Array.concat(),不多说:

<script>
var j = "我";
var k = "爱";
var l= "家";
document.write(j.concat(k,j,l));
document.write(j + k + j + l);
//显示:我爱我家 我爱我家
</script>
charAt()方法捕获指定位置的字符,charCodeAt()方法捕获指定位置的字符的Unicode编码,这两个方法都有一个参数n,表示从第几 个字符开始返回数据。fromCharCode()方法和charCodeAt()的作用刚好相反,可以把Unicode编码的字符转换成字符:

<script>
var m = "我爱我家";
document.write(m.charAt(0));
document.write(m.charCodeAt(0));
document.write(String.fromCharCode(25105, 29233, 25105, 23478));
//显示:我 25105 我爱我家
</script>
split()方法的作用是把字符串用特定的分隔符分隔成数组,split()方法有两个参数,第一个参数是分隔符(可以是正则),第二个参数表示需要返回的数组数,缺省时全部返回:

<script>
document.write("1:2:3:4:5".split(":"));
//返回["1","2","3","4","5"]
document.write("|a|b|c|".split("|"));
//返回["", "a", "b", "c", ""]
document.write("hello".split(""));
//返回["h","e","l","l","o"]
document.write("hello".split("", 3));
//返回["h","e","l"]
document.write("hello <b>world</b>".split(/(<[^>]*>)/));
//返回["hello ","<b>","world","</b>",""],IE显示不出来是IE的问题
</script>
indexOf()方法可以获取某个字符或字符串在String中出现的位置,lastIndexOf()的作用和indexOf()类似,只不过是从后 开始向前检索,indexOf()方法和lastIndexOf()方法都有两个参数,第一个参数表示需要查找的字符,第二个参数表示开始查找的位置,在 lastIndexOf()方法中,第二个参数也是从前往后计算的,第二个参数不支持负数的形式。

<script>
var n="我爱我家的作者是梁左";
document.write(n.indexOf("我"));//第一个字符是0
document.write(n.indexOf("我",1));
document.write(n.lastIndexOf("我"));
document.write(n.indexOf("作者"));
document.write(n.lastIndexOf("作者"));
document.write(n.indexOf("宋丹丹"));
//显示:0 2 2 5 5 -1
</script>
slice()方法,substr()方法和substring()方法的作用都是截取字符串,功能和用法相似,但也各有用处。这三种方法都有2个参数, 表示需要截取字符串的位置情况,slice()方法和substring()方法的两个参数表示字符串的绝对位置,substr()方法的第1个参数表示 绝对位置,后一个参数表示的是相对于第一个参数的位置:

<script>
var o="我爱我家的作者是梁左"
document.write(o.slice(2,3))//第一个字符是0
document.write(o.substring(2,3))
document.write(o.substr(2,3))
//显示:我 我 我家的
</script>
slice()方法的两个参数如果有负数,就表示是从后往前计算,substring()方法的两个参数可以颠倒,如果有负数的话计算机会被认为是 0,substr()方法在FireFox下面,如果第一个参数小于0,FireFox会从后往前的计数,IE则会把第一个参数当成0,第二个参数如果小 于0,计算机会认为是0。

<script>
var p="我爱我家的作者是梁左"
document.write(p.slice(-5,-3))//第一个字符是0
document.write(p.substring(-5,2))
document.write(p.substr(-7,3))
//显示:作者 我爱 家的作
</script>
search()方法使用正则表达式查询并返回第一次匹配的位置,search()方法只有1个参数:

<script>
var q="我爱我家的作者是梁左"
document.write(q.search("家"))//可以是正则
//显示:3
</script>
match()方法在匹配以后能够返回匹配的内容,如果和正则表达式很好的结合使用,能够拥有非常强大的功能。在match()方法不使用正则表达式的时候,返回匹配项的同时还返回匹配项出现位置(index)和整个字符串(input):

<script>
var r="我爱我家的作者是梁左"
document.write(r.match("家"))
document.write(r.match("家").index)
document.write(r.match("家").input)
//显示:家 3 我爱我家的作者是梁左
</script>
在使用正则表达式的时候有两种情况,一种是不带有全局匹配符“g”,另一种是带“g”,不带“g”的时候,返回的结果是一个数组,数组的第一项是匹配的结 果,如果正则中有()分隔,会依次将()所匹配的内容排列在数组里面,同时还返回匹配的位置(index)和整个字符串(input);带“g”的情况下 只返回匹配项的内容,然后依次组成数组,不返回位置和字符串,对于理解正则表达式的使用方法有困难的朋友请先阅读《JavaScript中的正则表达 式》:

<script>
var s = "http://www.w3c.org/index.php";
var t = /"w+/g;
u = s.match(t);
document.writeln(u[0]);
document.writeln(u[1]);
document.writeln(u[2]);
document.writeln(u.input);
document.writeln(u.index);
//带“g”的情况显示:http www w3c undefined undefined
var v = "http://www.w3c.org/index.php";
var w = /("w+):"/"/(["w".]+)"/(["w".]+)/;
x = v.match(w);
document.writeln(x[0]);
document.writeln(x[1]);
document.writeln(x[2]);
document.writeln(x[3]);
document.writeln(x.input);
document.writeln(x.index);
//不带“g”的情况显示:http://www.w3c.org/index.php http www.w3c.org index.php http://www.w3c.org/index.php 0
</script>
replace()方法可以对字符串中的内容进行匹配和替换,对于正则内部带有()的部分,可以使用逆向引用(backreferencing)的方法,使用“$1,$2,$3”的变量形式引用匹配的内容:

<script>
var y = "我喜欢我家";
z = y.replace("喜欢","爱");
document.writeln(z);
document.writeln(z.input);
document.writeln(z.index);
//显示:我爱我家 undefined undefined
var a = /("d{3})("d{2})("d{4})/;
var b = "123456789";
c = b.replace(a, "$1-$2-$3");
document.writeln(c);
//显示:123-45-6789
</script>
JavaScript中还有一个大类的String方法,称为HTML方法,只要懂得基本的HTML语法,就能看懂这些方法的使用规则:

anchor("name"):给链接加锚点
big():字体加大
blink():闪烁文字//IE不支持此方法
bold():粗体字
fixed():打字字体(字母宽度相同)
fontcolor("color"):字体颜色
Fontsize(size):字体大小
italics():斜体字
Link("location"):给文字加上链接
small():字体缩小
strike():画线删除字体
Sub():下标字
Sup():上标字

使用方法可以自己调试,语法列出如下:

<script>
document.write("text".anchor("name"));
document.write("text".big());
document.write("text".blink());
document.write("text".bold());
document.write("text".fixed());
document.write("text".fontcolor("red"));
document.write("text".fontsize(7));
document.write("text".italics());
document.write("text".link("#name"));
document.write("text".small());
document.write("text".strike());
document.write("text".sub());
document.write("text".sup());
</script>

posted @ 2009-04-27 09:45  Just for Fun  阅读(188)  评论(1编辑  收藏  举报