給出字符串是否以給定的字符結尾code
confirmEnding("He has to give me a new name", "name") should return true.
str.substr或者substr方法字符串
function confirmEnding(str, target) { return str.substr(str.length-target.length) === target ? true:false; } confirmEnding("Bastian", "n");
str.endswith() //ES6get
function confirmEnding(str, target) { return str.endswith(target); } confirmEnding("Bastian", "n");
substr(start,length) substring(start,end)
substr是從起始點截取某個長度的字符串string
substring是截取2個位置之間的字符串it
有其餘好的方法或思路或者對各方法有更深理解的道友,不妨在沙發區神交一番。io