JavaScript 1.3 String

本文深入探讨JavaScript中String对象的各种方法,包括构造函数、属性及多种字符串操作方法,如锚点创建、删除线显示、颜色设置、链接添加、大小写转换等,通过实例展示了如何使用这些方法进行高效字符串处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值