JavaScript 指定字串結尾

admin發表於2017-12-04

在實際字串操作中,可能需要判斷字串是否以指定的字串結尾。

下面就是一段能夠實現此功能的程式碼例項。

程式碼如下:

[JavaScript] 純文字檢視 複製程式碼執行程式碼
String.prototype.endWith=function(endStr){
  var d=this.length-endStr.length;
  return (d>=0&&this.lastIndexOf(endStr)==d);
}
var str="I love antzone";
console.log(str.endWith("ne"));

程式碼實現了我們的要求,可以判斷字串是否以指定字串結尾。

相關閱讀:

(1).lastIndexOf()參閱JavaScript String lastIndexOf()一章節。

(2).endWith()參閱JavaScript endsWith()一章節。

相關文章