Confirm the Ending

Posted 笨鸟房子

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Confirm the Ending相关的知识,希望对你有一定的参考价值。

检查一个字符串(str)是否以指定的字符串(target)结尾。

如果是,返回true;如果不是,返回false。

 

/*思路
  字符串长度str.length等于字符串位置str.indexOf() + 字符串长度target.length;
  为避免字符串中重复的target所以应从后往前搜索lastIndexOf() ;
*/

function confirmEnding(str, target) {
  if(str.lastIndexOf(target)+target.length!=str.length){
    return false;
  }
  return true;
}

confirmEnding("Bastian", "n");

 

以上是关于Confirm the Ending的主要内容,如果未能解决你的问题,请参考以下文章