JavaScript 1.3 String

一、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>

猜你喜欢

转载自blog.csdn.net/LetsStudy/article/details/84941490
1.3