js中startWith和endWith的擴充套件
阿新 • • 發佈:2019-02-01
今天用到了js中的endWith方法,以為直接有呢,就直接用了,結果報錯,endWith 未定義啥的,後來發現這個startWith和endWith都需要另外實現一下,只要加上以下程式碼就行了。
String.prototype.endWith = function(str){ if(str==null || str=="" || this.length == 0 ||str.length > this.length){ return false; } if(this.substring(this.length - str.length)){ return true; }else{ return false; } return true; }; String.prototype.startWith = function(str){ if(str == null || str== "" || this.length== 0 || str.length > this.length){ return false; } if(this.substr(0,str.length) == str){ return true; }else{ return false; } return true; };