一、String方法示例
<script>
var str = new Ren('王二' ,25);
function Ren(name, age) {
this.name = name;
this.age = age;
}
Ren.prototype.shen = "学生" ;/*给Ren对象添加公用的属性shen和公用的方法get()*/
Ren.prototype.my = get();
document.write(str.constructor);/*constructor返回创建对象函数的引用*/
function get() {
return '你好';
}
document.write(str.my);
var st = new String ('hello world');
document.write(st.anchor("myanchor"));/*写入一个锚点a,name为myanchor*/
document.write(st.strike());/*使用删除线来显示字符串。*/
document.write(st.fontcolor('red'));/*设置文字的颜色*/
document.write(st.link("http://www.baidu.com"));/*给字符串加跳转链接*/
document.write(st.toUpperCase());/*将字符串全部变为大写*/
document.write(st.fixed());/*以打字机文本显示字符串*/
document.write(st.charAt(1));/*返回位置为1的字符*/
document.write(st.concat(" I come from China"));/*返回连接后的字符串*/
document.write(st.indexOf('r'));/*检索字符串,返回它的位置;lastIndexOf从后向前检索*/
document.write(st.replace("o","k"));/*返回用k替换字符串中的o的新字符串*/
document.write(st.match("o"));/*返回匹配字符串中的o,没有返回null*/
</script>
本文深入探讨JavaScript中String对象的各种方法,包括构造函数、属性及多种字符串操作方法,如锚点创建、删除线显示、颜色设置、链接添加、大小写转换等,通过实例展示了如何使用这些方法进行高效字符串处理。
1万+

被折叠的 条评论
为什么被折叠?



