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

                ### String * toString() 將其他類型(不包括null和undefined)轉換為string類型的字符串 * String() 將其他類型(包括null和undefined)轉換為string類型的字符串 * indexOf(),lastIndexOf(),includes(), startsWith(), endsWith() 字符串查找 * charAt(),charCodeAt() 字符操作方法 * slice(),substr(),substring() 字符串操作方法 * trim() 刪除字符串空格 * toLowerCase(),toLocaleLowerCase(),toUpperCase(),toLocaleUpperCase() 字符串大小寫轉換 * localCompare() 字符串比較 * 模版字符串 * * * * * #### toString() > String用于表示由0或多個16位Unicode編碼字符組成的字符虛列,即字符串,在javascript中字符串可以由“”或‘’表示,但很多時候我們需要把其他的數據類型轉為字符串類型,因此javascript為我們提供了toString()方法,除了null和undefined沒有toString()方法外其他所有數據類型都有,同時toString()方法還可以傳入一個參數,該參數的作用是指定輸出的類型,參數可以傳(2,8,10,16)對應返回二進制,八進制,十進制和十六進制,具體使用方式如下: ~~~ let v1 = 10 console.log(v1.toString()) // 10 let v2 = true console.log(v2.toString()) // true let v3 = 10 console.log(v3.toString(2)) // 1010 console.log(v3.toString(8)) // 12 console.log(v3.toString(10)) // 10 console.log(v3.toString(16)) // a ~~~ #### String() > 那么問題來了,如何知道某個要轉型的值是不是null或者undefined呢? > 還可以使用轉型函數String(),這個函數能將任何類型的值轉為字符串,包括null和undefined,代碼如下: ~~~ let v1 = 10 console.log(String(v1)) // 10 let v2 = true console.log(String(v2)) // true let v3 = null console.log(String(v3)) // null let v4 console.log(String(v4)) // undefined ~~~ #### indexOf(),lastIndexOf(),includes(), startsWith(), endsWith() > indexOf() , lastIndexOf(),這兩個方法都是從一個字符串中搜索給定的子字符串,然后返回子字符串的位置(如果沒有找到子字符串則返回-1),這兩個方的區別是:indexOf()是從字符串開頭向后搜索,lastIndexOf()則是從字符串末尾向前搜索,代碼如下: ~~~ let s = "Hello world!" console.log(s.indexOf("H")) //0 let s = "Hello world!" console.log(s.lastIndexOf("H")) //10 ~~~ > includes():返回布爾值,表示是否找到了參數字符串。 ~~~ let s = "Hello world!" console.log(s.includes("Hello")) //true ~~~ >startsWith():返回布爾值,表示參數字符串是否在源字符串的頭部。 ~~~ let s = "Hello world!" console.log(s.startsWith("Hello")) //true ~~~ >endsWith():返回布爾值,表示參數字符串是否在源字符串的尾部。 ~~~ let s = "Hello world!" console.log(s.endsWith("Hello")) //false ~~~ #### charAt(),charCodeAt() > charAt(),charCodeAt()是javascript中用于訪問字符串中特定字符的方法,這兩個方法都接收一個參數,即基于0但字符位置,其中charAt()方法以單字符形式返回給定位置的那個字符,例如: ~~~ let s = "hello world" console.log(s.charAt(1)) // e ~~~ > 如果你想得到的是字符編碼而不是字符串,那可以使用charCodeAt()方法,如下: ~~~ let s = "hello world" console.log(s.charCodeAt(1)) // 101 ~~~ #### slice(),substr(),substring() > javascript提供了slice(),substr(),substring()這三個方法來操作字符串,這三個方法都返回一個字符串值,而且也都接收一個或兩個參數,第一個參數(在沒有指定第二個參數的情況下)指定返回字符串的起始位置,第二個參數(在指定情況下)表示返回字符串的結束位置,具體來說slice(),substr()對第二個參數指定的是子字符串最后一個字符串的位置,而substring()對第二個參數指定的則是返回的字符串個數,如果沒有給這些方法第二個參數,就相當于指定字符串長度為結束位置。 >與concat()一樣,這三個方法并不會改變原始字符串,具體使用方法如下: ~~~ let s ="hello andy" console.log(s.slice(3)) // "lo andy" console.log(s.substring(3)) // "lo andy" console.log(s.substr(3)) // "lo andy" console.log(s.slice(3,7)) // "lo a" console.log(s.substring(3,7)) // "lo a" console.log(s.substr(3,7)) // "lo andy" ~~~ > 需要注意的是,在傳遞的參數是負值的情況下,他們的行為就不盡相同了,slice()方法回將傳入的負值與字符串的長度相加,substr()方法回將負的第一個參數加上字符串長度,而將負的的二個參數轉換為0,substring()方法會將所有的負值參數都轉換為0,代碼如下: ~~~ let s ="hello andy" console.log(s.slice(-3)) // "ndy" console.log(s.substring(-3)) // "hello andy" console.log(s.substr(-3)) // "ndy" console.log(s.slice(3,-3)) // "lo a" console.log(s.substring(3,-3)) // "hel" console.log(s.substr(3,-3)) // " " ~~~ #### trim() > trim()方法會創建一個字符串的副本,刪除前置和后綴的所有空格,與上面方法一樣,該方法并不會改變字符串原始值,代碼如下: ~~~ let s = " andy " console.log(s.trim()) // "andy" ~~~ #### toLowerCase(),toLocaleLowerCase(),toUpperCase(),toLocaleUpperCase() > 在javascript中需要轉換字符串的大小寫可以使用toLowerCase(),toLocaleLowerCase(),toUpperCase(),toLocaleUpperCase() 這四個方法,代碼如下: ~~~ let s = "andy" let i = "ANDY" console.log(s.toLowerCase()) // "ANDY" console.log(s.toLocaleLowerCase()) // "ANDY" console.log(i.toUpperCase()) // "andy" console.log(i.toLocaleUpperCase()) // "andy" ~~~ #### localCompare() > localCompare()這個方法用來比較兩個字符串,如果字符串在字母表中的位置排在參數之前,則返回一個負數(大多返回-1,具體視情況而定),如果字符串等于參數,則返回0,如果字符串在字母表中的位置排在參數之后,則返回一個正數(大多是1,具體視情況而定),代碼如下: ~~~ let s = "jack" console.log(s.localCompare("tom")) // -1 console.log(s.localCompare("andy")) // 1 console.log(s.localCompare("jack")) // 0 ~~~ #### 模版字符串 > 在es6之前,我們的網頁通過ajax加載的數據需要渲染在網頁中是通過創建dom以及字符串拼接的方式插入到dom節點中,這樣做非常不方便,es6為我們提供了模版字符串來解決這個問題,模板字符串(template string)是增強版的字符串,用反引號(`)標識。它可以當作普通字符串使用,也可以用來定義多行字符串,或者在字符串中嵌入變量。示列如下: ~~~ // 普通字符串 `In JavaScript '\n' is a line-feed.` // 多行字符串 `In JavaScript this is not legal.` console.log(`string text line 1 string text line 2`); // 字符串中嵌入變量 let name = "Bob", time = "today"; `Hello ${name}, how are you ${time}?` ~~~ > 上面代碼中的模板字符串,都是用反引號表示。 >如果在模板字符串中需要使用反引號,則前面要用反斜杠轉義。 >使用模板字符串表示多行字符串,所有的空格和縮進都會被保留在輸出之中。你也可以通過trim()方法消除空格和縮進。 >模板字符串中嵌入變量,需要將變量名寫在${}之中。 >( {} )大括號內部可以放入任意的JavaScript表達式,可以進行運算,以及引用對象屬性。 >通過 {} 模板字符串之中還能調用函數。 >如果大括號中的值不是字符串,將按照一般的規則轉為字符串。 >比如,大括號中是一個對象,將默認調用對象的toString方法。 >由于模板字符串的大括號內部,就是執行JavaScript代碼,因此如果大括號內部是一個字符串,將會原樣輸出。
                  <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>

                              哎呀哎呀视频在线观看