📜  JavaScript String endsWith() 方法

📅  最后修改于: 2022-05-13 01:56:26.725000             🧑  作者: Mango

JavaScript String endsWith() 方法

下面是 String endsWith() 方法的示例。

  • 例子:
    
    
  • 输出:
    true
    

str.endsWith()函数用于检查给定字符串的字符结尾。

句法:

str.endsWith(searchString, length)

论据:
这个函数的第一个参数是一个字符串字符 ,它将在给定字符串的末尾进行搜索。该函数的第二个参数是长度,它确定给定字符串从开始搜索searchString的长度。

返回值:
如果找到 searchString,此函数返回布尔值true ,否则返回布尔值false

下面提供了上述函数的示例:
示例 1:

var str = 'It is a great day.'
print(str.endsWith('day.'));

输出:

true

在此示例中,函数endsWith()检查给定字符串末尾的searchString 。由于在末尾找到了searchString ,因此该函数返回true

示例 2:

var str = 'It is a great day.'
print(str.endsWith('great'));

输出:

false

在此示例中,函数endsWith()检查给定字符串末尾的searchString 。由于最后没有找到searchString ,因此该函数返回false

示例 3:

var str = 'It is a great day.'
print(str.endsWith('great',13));

输出:

true

在此示例中,函数endsWith()检查给定字符串末尾的searchString 。由于第二个参数在索引13处定义了字符串的结尾,并且在该索引处 searchString被发现结束,因此该函数返回true

上述函数的代码如下:

方案一:


print(value); 

输出:

true

方案二:

// JavaScript to illustrate endsWith() function

输出:

false

方案 3:

 

输出:

true

支持的浏览器:

  • 铬 41 及以上
  • 边缘 12 及以上
  • 火狐 17 及以上
  • Opera 28 及以上
  • Safari 9 及更高版本