ES6 - 新字串方法



以下是方法列表及其描述。

序號 方法及描述
1 String.prototype.startsWith(searchString, position = 0)

如果接收者以 searchString 開頭則返回 true;position 允許您指定要檢查的字串的起始位置。

2 String.prototype.endsWith(searchString, endPosition = searchString.length)

如果接收者以 searchString 開頭則返回 true;position 允許您指定要檢查的字串的起始位置。

3 String.prototype.includes(searchString, position = 0)

如果接收者包含 searchString 則返回 true;position 允許您指定要搜尋的字串的起始位置。

4 String.prototype.repeat(count)

返回接收者,連線 count 次。

模板字面量

模板字面量是允許嵌入表示式的字串字面量。模板字串使用反引號 (``) 而不是單引號或雙引號。因此,模板字串可以寫成 -

var greeting = `Hello World!`; 

字串插值和模板字面量

模板字串可以使用佔位符透過 ${ } 語法進行字串替換,如下所示。

示例 1

var name = "Brendan"; 
console.log('Hello, ${name}!');

在成功執行上述程式碼後,將顯示以下輸出。

Hello, Brendan!

示例 2:模板字面量和表示式

var a = 10; 
var b = 10; 
console.log(`The sum of ${a} and ${b} is  ${a+b} `);

在成功執行上述程式碼後,將顯示以下輸出。

The sum of 10 and 10 is 20 

示例 3:模板字面量和函式表示式

function fn() { return "Hello World"; } 
console.log(`Message: ${fn()} !!`);

在成功執行上述程式碼後,將顯示以下輸出。

Message: Hello World !!

多行字串和模板字面量

模板字串可以包含多行。

示例

var multiLine = `
   This is 
   a string 
   with multiple 
   lines`; 
console.log(multiLine)

在成功執行上述程式碼後,將顯示以下輸出。

This is 
a string 
with multiple 
line

String.raw()

ES6 包含用於原始字串的標籤函式 String.raw,其中反斜槓沒有特殊含義。String.raw 使我們能夠像在正則表示式字面量中一樣編寫反斜槓。請考慮以下示例。

var text =`Hello \n World` 
console.log(text)  

var raw_text = String.raw`Hello \n World ` 
console.log(raw_text)

在成功執行上述程式碼後,將顯示以下輸出。

Hello 
World 
Hello \n World

標記模板

標記是一個函式,它可以解釋和處理模板字面量。標記出現在模板字面量之前。語法如下所示。

語法

let output_fromTag = tagFunction `Template literal with ${variable1} , ${variable2}`

標記函式實現語法如下所示 -

function tagFunction(literals,...variable_values){
   //process
   return "some result"
}

示例

以下示例定義了一個標記函式 myTagFn()。它顯示傳遞給它的引數。顯示後,它將 Done 返回給呼叫方。

<script>
   function myTagFn(literals,...values){
      console.log("literal values are");
      for(let c of literals){
         console.log(c)
      }

      console.log("variable values are ");
      for(let c of values){
         console.log(c)
      }

      return "Done"
   }
   let company = `TutorialsPoint`
   let company_location = `Mumbai`
   let result = myTagFn `Hello this is ${company} from ${company_location}`

   console.log(result)

</script>

上述程式碼的輸出將如下所示 -

//literal
literal values are
Hello this is
from
//values
variable values are
TutorialsPoint
Mumbai
Done

示例

下面的 標記函式獲取一個 模板字面量並將其轉換為大寫,如下所示 -

<script>
   function convertToUpperTagFn(literals, ...values) {
      let result = "";
      for (let i = 0; i < literals.length; i++) {
         result += literals[i];
         if (i < values.length) {
            result += values[i];
         }
      }
      return result.toUpperCase();
   }
   let company = `TutorialsPoint`
   let company_location = `Mumbai`
   let result = convertToUpperTagFn `Hello this is ${company} from ${company_location}`

   console.log(result)

</script>

上述程式碼的輸出將如下所示 -

HELLO THIS IS TUTORIALSPOINT FROM MUMBAI

String.fromCodePoint()

靜態 String.fromCodePoint() 方法返回一個使用指定的 unicode 程式碼點序列建立的字串。如果傳遞了無效的程式碼點,則該函式會丟擲 RangeError。

console.log(String.fromCodePoint(42))        
console.log(String.fromCodePoint(65, 90))

在成功執行上述程式碼後,將顯示以下輸出。

* 
AZ
廣告

© . All rights reserved.