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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章