确认字符串的结尾(结尾长度可变)

3
这个任务需要你编写一个函数,它接受两个参数。第一个参数是一个名为str的字符串,第二个参数是我们目标结尾的字符串target。任务是验证str的结尾是否与目标结尾相同。指令要求使用.substr()方法将结尾与目标进行比较。问题在于.substr方法会有多个起始点和长度参数,因为目标结尾的长度可能不同。请看一下我解决这个问题的尝试,并指导我正确的方向。
function end(str, target) {
  var start = str.length - (target.length - 1);
  if(str.substr(start, str.length) == target){
     return true;
  } else {
     return false;
  }
}

end('Bastian', 'n');

1
如果 var start = str.length - target.length;,你的代码应该可以正常工作。 - rzr
可能是 JavaScript 中的 endsWith 的重复问题。 - user663031
@torazaburo不是DUP:用户询问_指令表明使用.substr()方法将结尾与目标进行比较_...这个问题与endWidth无关。 - rnrneverdies
@oneway 嗯,这取决于人们如何狭义地解释问题。这个问题有一个更广泛的背景,即如何做到类似于“endsWith”的功能;它有一个更狭窄的背景,即如何以一种特定的、次优的方式实现。在我看来,建议提供替代的、更好的方法来解决基本问题是合法的范围之内。如果一个问题问的是同样基本的问题,尤其是在这种情况下,被认为是重复的问题确实提供了一个基于substr的正确实现答案,那么这个问题就重复的。 - user663031
11个回答

6

编辑

正如@torazaburo所说,正确答案是:

function end(str, target) {
    return target === str.substr(str.length - target.length);
}

因为字符串确实以空字符串结尾

原始答案

function end(str, target) {
    return target.length > 0 && target === str.substr(str.length - target.length);
}

This code is using the && operator to compare two conditions. The left side, target.length > 0, should always be true with a valid input for target. The right side sets target equal to a substring of str that starts at the position determined by subtracting the length of target from the length of str (the far right position). Since there is no end point specified, the substring will run to the end of str. HTML tags are preserved. You can find the original code at http://jsfiddle.net/tqsx0gLa/2/.

1
这是正确的答案。对于新手来说,解释可能有所帮助,所以让我来解释一下。此代码使用 '&&' 运算符设置逻辑比较。左侧的 'target.length > 0' 应始终返回 true,对于有效的目标输入。左侧将目标设置为 'substr',从 'str.length'(即 'str' 最右边的位置)减去 'target' 长度(以到达我们子字符串的起点)。由于子字符串将运行到 str 的末尾,因此不需要一个最终输入。 - Austin Hansen
为什么需要 target.length > 0 - user663031
案例4应该返回true。该字符串确实以空字符串结尾。 - user663031

1

Here is a easy solution :

function confirmEnding(str, target) {

  var result;

  //getting the last string depend on target length 
  var last = str.substring(str.length - target.length);   

  //checking the last string with the target
  if(last === target){ 

     result = true;

  } else {

   result = false;
  }

return result;
}

在头部声明一个变量并使用if语句将布尔值存储在其中 - 是否有更复杂的方法来编写return last === target - Moritz Ringler

0

我觉得这很简单:

function end(str, target) {
  return str.substr(-target.length) == target;
}

0
function confirmEnding(str, target) {
   var position = str.length - target.length; //get start position for our .substr method.....

   if (str.substr(position, target.length == target){ // logical expression wich tell our method(.substr) from which position we need get information then we compare result with our "target" argument.
     return true;
    } else { return false; }

}

confirmEnding("Bastian", "n"); // true 确认结尾("Bastian", "n"); // 真

添加一些细节以帮助理解问题以及为什么此代码解决了该问题。 - Naren Sisodiya

0

我喜欢你的原始答案,它干净易读。 尝试在第二行删除-1。这样它将返回你的substr中所有的目标单词。

    function end(str, target) {
  var start = str.length - (target.length);
  if(str.substr(start) == target){
     return true;
  } else {
     return false;
  }
}
end ("He has to give me a new name", "name")

当子字符串没有返回第二个参数时,它将从start的数字返回到字符串的末尾。


0

给你:

const solution = (str, target) => str.endsWith(target);

最清晰的方式。

0

`

function endSearch(str, ending){
  // just on line to solve this problem 
  // endsWith() ==> string.endsWith('string search' , position = str.length['defalut value'])
   return str.endsWith(ending)
}
console.log(endSearch('abc', 'bc')) // ture 
console.log(endSearch('abc', 'a')) // false
console.log(endSearch('abc', 'x')) // false

`


1
你的回答可以通过提供更多支持信息来改进。请编辑以添加进一步的细节,例如引用或文档,以便他人可以确认你的答案是正确的。您可以在帮助中心找到有关如何编写良好答案的更多信息。 - Community

0
我的解决方案使用了 substr 方法,它接受目标字符串的长度,并且通过减法运算给出第一个字符串的最后几个字符以进行比较。
const confirmEnding = (str, target) => str.substr(-target.length) === target ? 
                                                                      true : 
                                                                      false;

0

function confirmEnding(str, target) {
  let ending = str.slice(str.length-target.length)
  return ending == target
}

console.log(confirmEnding("Congratulation", "on"))


0
function end(str, target) {
  var start = str.length - target.length;

  if(str.substr(start) == target){
    return true;
  }
  else {
    return false;
  }
}

你也可以尝试这段代码。


1
为什么你不写 return str.substr(start) == target; 呢? - user663031
很酷。我猜这个更明确一些。但是你的更有效率。 - codebrew85

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接