JavaScript[19] -- 字符串

字符串方法

length

  • length長度,字符串的某一項[ ],注意:字符串長度只能讀取,不能修改,[ ]在IE7及如下不識別數組

    <script>
        let str = "abcd";
        str.length = 2;     // 不會生效
        console.log(str.length );   // 4
        console.log(str);   // abcd
    </script>

charAt()

  • 取某一個字符串項,能夠代替[ ]取下標,可是IE8及如下不兼容編碼

    <script>
        let str = "abcd";
        console.log(str.charAt(0));   // a
        console.log(str.charAt(5));   // 結果爲空格,不是""
        console.log(str.charAt(str.length-1));    // d 獲取最後一項
        console.log(typeof(str.charAt(5)));       // string        
    </script>

charCodeAt()

  • 返回指定位置的字符的unicode編碼code

    <script>
        let str = "abcd";
        console.log(str.charCodeAt(0));   // 97
        console.log(str.charCodeAt(5));   // NaN
        console.log(typeof(str.charCodeAt(0)));   //  number
        console.log(typeof(str.charCodeAt(5)));   //  number
    </script>

String.fromCharCode()

  • 經過unicode編碼返回對應的字符ip

    <script>
        let a = String.fromCharCode(108),
            b = String.fromCharCode(111),
            c = String.fromCharCode(118),
            d = String.fromCharCode(101);
        console.log(a + b + c + d);     // love
    </script>

indexOf() 檢索

  • 一個參數時,返回指定字符串的位置
  • 兩個參數時,第一個參數爲檢索值,第二個參數爲檢索開始的位置,只找第一個,而且返回對應的下標值,若是沒有返回-1unicode

    <script>
        let str = "abcda";
        console.log(str.indexOf("c"));      // 2    表示str中第一個"c"在第3個位置上
        console.log(str.indexOf(5));        // -1   表示str中沒有5
    
        console.log(str.indexOf("a", 0));        // 0 表示從str的第一位開始檢索,第一個a在str第一個位置上
    </script>

lastIndexOf() 從後往前檢索

  • 一個參數時,返回最後一次出現的位置
  • 兩個參數時,第一個爲檢索的值,第二個爲檢索的位置字符串

    <script>
         let str = "Hello world";
        console.log(str.lastIndexOf("Hello"));      // 0    表示str中最後一個"Hello"的起始位第0個位
        console.log(str.lastIndexOf("World"));      // -1   表示str中沒有"World"
    
        console.log(str.lastIndexOf("Hello", 5));       // 0 表示去除str的最後5位字母,剩下的子母中,Hello的起始位置爲第0位
    </script>

substring() 截取字符串

  • 第一個參數爲起始位置(包含),第二個參數爲結束位置(不包含),若是第二個參數不寫,會默認截取全部的
  • 當有兩個參數時,結果和參數的順序無關,只和參數大小有有關,小的爲開始位置,大的爲截取長度string

    <script>
         let str = "Hello world";
         console.log(str.substring(0, 2));   // He       在str中,從第0位開始截取2位
         console.log(str.substring(2, 0));   // He       不是從第2位開始截取0位,跟參數順序無關,跟參數大小有關,小的爲開始位置,打的爲截取長度
         console.log(str.substring(6));      // world    在str中,從第6位開始截取到最後
    </script>

slice() 截取字符串

  • 和substring同樣,可是數字大的不能在前it

    <script>
        console.log(str.slice(0, 2));           // He      
        console.log(str.slice(2, 0));           // 結果爲空格         
        console.log(typeof(str.slice(2, 0)));   // string
        console.log(str.slice(6));              // world    
    </script>

split() 切割成數組

  • 字符串變數組,從某一項切割,先後變爲字符串項console

    <script>
        let num = "1,2,4,5";
        console.log(num.split(","));    // ["1", "2", "4", "5"] 把逗號去掉,而且逗號先後分開變爲數組項
        console.log(num.split(""));     // ["1", ",", "2", ",", "4", ",", "5"] 把每一位都做爲數組項
    
        let str = "abacda";
        console.log(str.split("a"));   // ["", "b", "cd", ""] 把a做爲數組分割符,將"a"的先後項依次做爲數組項
    </script>

toLowerCase() / toUpperCase()

  • 大寫轉小寫 / 小寫轉大寫ast

    <script>
        let num = "1,2,4,5";
        console.log(num.toLowerCase());    // 1,2,4,5
      
        let str = "Hello world 1";
        console.log(str.toLowerCase());    // hello world 1 
        console.log(str.toUpperCase());    // HELLO WORLD 1 
    </script>
相關文章
相關標籤/搜索