ES6 - 新的字符串方法

以下是方法及其说明的列表。

Sr.No 方法和说明
1 String.prototype.startsWith(searchString, position = 0)

如果接收器以 searchString 开头,则返回 true;位置允许您指定要检查的字符串的开始位置。

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

如果接收器以 searchString 开头,则返回 true;位置允许您指定要检查的字符串的开始位置。

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

如果接收器包含 searchString,则返回 true;位置让您指定要搜索的字符串的开始位置。

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 
 World` 
console.log(text)  

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

成功执行上述代码后将显示以下输出。

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