学习ES6(十六)-- ES6中新的字符串方法

序号方法与说明
1 String.prototype.startWith(searchString,position=0)

如果接收方以searchString开头,则返回true;否则,返回false。position可以指定要检查的字符串的起始位置。

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

如果接收方以searchString结尾,则返回true;否则,返回false。position可以指定要检查的字符串的起始位置。

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

如果接收方包含searchString,则返回true;否则,返回true。position可以指定要搜索的字符串的起始位置。

4 String.prototype.repeat(count)

返回接收器,并置的计数时间。

startsWith方法确定字符串是否以指定的字符开头。 
语法:str.startsWith(searchString[, position])

参量

  • searchString-在此字符串开头要搜索的字符。

  • postion -此字符串中开始搜索searchString的位置;默认为0。

返回值

如果字符串以搜索字符串的字符开头,则为true;否则为false。否则为false

var str = 'hello world!!!'; 
console.log(str.startsWith('hello')); //输出:true

endsWith函数确定一个字符串是否以另一个字符串的字符结尾。

语法:str.endsWith(matchstring[, position])

参量

  • matchstring-字符串必须以结尾的字符。区分大小写。

  • position-匹配字符串的位置。此参数是可选的。

返回值

如果字符串以匹配字符串的字符结尾,则为true;否则为true。否则为false

var str = 'Hello World !!! '; 
console.log(str.endsWith('Hello'));  //输出:false
console.log(str.endsWith('Hello',5)); //输出:true

includes方法确定字符串是否是给定字符串的子字符串。

语法:str.includes(searchString[, position])

参量

  • searchString-要搜索的子字符串。

  • postion-此字符串中开始搜索searchString的位置;默认为0。

返回值

如果字符串包含子字符串,则为true;否则为false 否则为false

var str = 'Hello World';  

console.log(str.includes('hell'))      //输出:false
console.log(str.includes('Hell'));  //输出:true

console.log(str.includes('or'));   //输出:true
console.log(str.includes('or',1))//输出:true

模板文字

模板文字是允许嵌入表达式的字符串文字。模板字符串使用反引号(``)而不是单引号或双引号。模板字符串因此可以写成-

var greeting = `Hello World!`; 

字符串插值和模板文字

示例2:模板字符串可以使用占位符使用$ {}语法进行字符串替换。

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()。它显示传递给它的参数。显示后,将result返回给呼叫者。

<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

String.fromCodePoint()

静态字符串。fromCodePoint()方法返回通过使用指定的Unicode代码点序列创建的字符串。如果传递了无效的代码点,该函数将引发RangeError。

console.log(String.fromCodePoint(42))        //输出:* 
console.log(String.fromCodePoint(65, 90)) //输出:AZ
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值