<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                ## 一、概述 `String`對象是 JavaScript 原生提供的三個包裝對象之一,用來生成字符串對象。 ~~~ var s1 = 'abc'; var s2 = new String('abc'); typeof s1 // "string" typeof s2 // "object" s2.valueOf() // "abc" ~~~ 上面代碼中,變量`s1`是字符串,`s2`是對象。由于`s2`是字符串對象,`s2.valueOf`方法返回的就是它所對應的原始字符串。 ## 二、靜態方法 ### 2.1 String.fromCharCode() 該方法的參數是一個或多個數值,代表 Unicode 碼點,返回值是這些碼點組成的字符串。 ~~~ String.fromCharCode() // "" String.fromCharCode(97) // "a" String.fromCharCode(104, 101, 108, 108, 111) // "hello" ~~~ ## 三、實例屬性 ### 3.1 String.prototype.lenth 字符串實例的`length`屬性返回字符串的長度。 ~~~ 'abc'.length // 3 ~~~ ## 四、實例方法 ### 4.1 String.prototype.charAt() `charAt`方法返回指定位置的字符,參數是從`0`開始編號的位置。 ~~~ var s = new String('abc'); s.charAt(1) // "b" s.charAt(s.length - 1) // "c" ~~~ 如果參數為負數,或大于等于字符串的長度,`charAt`返回空字符串。 ~~~ 'abc'.charAt(-1) // "" 'abc'.charAt(3) // "" ~~~ ### 4.2 String.prototype.charCodeAt() `charCodeAt`方法返回字符串指定位置的 Unicode 碼點(十進制表示),相當于`String.fromCharCode()`的逆操作。 ~~~ 'abc'.charCodeAt(1) // 98 ~~~ 上面代碼中,`abc`的`1`號位置的字符是`b`,它的 Unicode 碼點是`98`。 如果參數為負數,或大于等于字符串的長度,`charCodeAt`返回`NaN`。 ~~~ 'abc'.charCodeAt(-1) // NaN 'abc'.charCodeAt(4) // NaN ~~~ ### 4.3 String.prototype.concat() `concat`方法用于連接兩個字符串,返回一個新字符串,不改變原字符串。 ~~~ var s1 = 'abc'; var s2 = 'def'; s1.concat(s2) // "abcdef" s1 // "abc" ~~~ 該方法可以接受多個參數。 ~~~ 'a'.concat('b', 'c') // "abc" ~~~ ### 4.4 String.prototype.slice() `slice`方法用于從原字符串取出子字符串并返回,不改變原字符串。它的第一個參數是子字符串的開始位置,第二個參數是子字符串的結束位置(不含該位置)。 ~~~ 'JavaScript'.slice(0, 4) // "Java" ~~~ 如果省略第二個參數,則表示子字符串一直到原字符串結束。 ~~~ 'JavaScript'.slice(4) // "Script" ~~~ 如果參數是負值,表示從結尾開始倒數計算的位置,即該負值加上字符串長度。 ~~~ 'JavaScript'.slice(-6) // "Script" 'JavaScript'.slice(0, -6) // "Java" 'JavaScript'.slice(-2, -1) // "p" ~~~ 如果第一個參數大于第二個參數,`slice`方法返回一個空字符串。 ~~~ 'JavaScript'.slice(2, 1) // "" ~~~ ### 4.5 String.prototype.substring() `substring`方法用于從原字符串取出子字符串并返回,不改變原字符串,跟`slice`方法很相像。它的第一個參數表示子字符串的開始位置,第二個位置表示結束位置(返回結果不含該位置)。 ~~~ 'JavaScript'.substring(0, 4) // "Java" ~~~ 如果省略第二個參數,則表示子字符串一直到原字符串的結束。 ~~~ 'JavaScript'.substring(4) // "Script" ~~~ 如果第一個參數大于第二個參數,`substring`方法會自動更換兩個參數的位置。 ~~~ 'JavaScript'.substring(10, 4) // "Script" // 等同于 'JavaScript'.substring(4, 10) // "Script" ~~~ 如果參數是負數,`substring`方法會自動將負數轉為0。 ~~~ 'JavaScript'.substring(-3) // "JavaScript" 'JavaScript'.substring(4, -3) // "Java" ~~~ ### 4.6 String.prototype.substr() `substr`方法用于從原字符串取出子字符串并返回,不改變原字符串,跟`slice`和`substring`方法的作用相同。`substr`方法的第一個參數是子字符串的開始位置(從0開始計算),第二個參數是子字符串的長度。 ~~~ 'JavaScript'.substr(4, 6) // "Script" ~~~ 如果省略第二個參數,則表示子字符串一直到原字符串的結束。 ~~~ 'JavaScript'.substr(4) // "Script" ~~~ 如果第一個參數是負數,表示倒數計算的字符位置。如果第二個參數是負數,將被自動轉為0,因此會返回空字符串。 ~~~ 'JavaScript'.substr(-6) // "Script" 'JavaScript'.substr(4, -1) // "" ~~~ ### 4.7 String.prototype.indexOf(), String.prototype.lastIndexOf() `indexOf`方法用于確定一個字符串在另一個字符串中第一次出現的位置,返回結果是匹配開始的位置。如果返回`-1`,就表示不匹配。 ~~~ 'hello world'.indexOf('o') // 4 'JavaScript'.indexOf('script') // -1 ~~~ `indexOf`方法還可以接受第二個參數,表示從該位置開始向后匹配。 ~~~ 'hello world'.indexOf('o', 6) // 7 ~~~ `lastIndexOf`方法的用法跟`indexOf`方法一致,主要的區別是`lastIndexOf`從尾部開始匹配,`indexOf`則是從頭部開始匹配。 ~~~ 'hello world'.lastIndexOf('o') // 7 ~~~ 另外,`lastIndexOf`的第二個參數表示從該位置起向前匹配。 ~~~ 'hello world'.lastIndexOf('o', 6) // 4 ~~~ ### 4.8 String.prototype.trim() `trim`方法用于去除字符串兩端的空格,返回一個新字符串,不改變原字符串。 ~~~ ' hello world '.trim() // "hello world" ~~~ 該方法去除的不僅是空格,還包括制表符(`\t`、`\v`)、換行符(`\n`)和回車符(`\r`)。 ### 4.9 String.prototype.toLowerCase(), String.prototype.toUpperCase() `toLowerCase`方法用于將一個字符串全部轉為小寫,`toUpperCase`則是全部轉為大寫。它們都返回一個新字符串,不改變原字符串。 ~~~ 'Hello World'.toLowerCase() // "hello world" 'Hello World'.toUpperCase() // "HELLO WORLD" ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看