admin管理员组

文章数量:1290234

This task requires that you write a function that takes two arguments. The first argument is a string called str and the second argument is a string that our target ending named target. The task is to verify that the ending of str is identical to the target ending. The instructions indicate to use the .substr() method to pare the endings to the targets. The problem I have is that there are going to be multiple starting points and length arguments for the .substr method since the target endings can be of variable length. Take a look at my attempt to solve this issue and please direct me in the right path.

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');

This task requires that you write a function that takes two arguments. The first argument is a string called str and the second argument is a string that our target ending named target. The task is to verify that the ending of str is identical to the target ending. The instructions indicate to use the .substr() method to pare the endings to the targets. The problem I have is that there are going to be multiple starting points and length arguments for the .substr method since the target endings can be of variable length. Take a look at my attempt to solve this issue and please direct me in the right path.

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');
Share Improve this question edited Jul 5, 2015 at 7:46 user663031 asked Jun 28, 2015 at 1:55 Austin HansenAustin Hansen 3741 gold badge6 silver badges18 bronze badges 4
  • 1 Your code should work fine if var start = str.length - target.length; – MayThrow Commented Jun 28, 2015 at 2:48
  • possible duplicate of endsWith in javascript – user663031 Commented Jul 5, 2015 at 7:33
  • @torazaburo is not a DUP: the user asked The instructions indicate to use the .substr() method to pare the endings to the targets ... this questios is not about endWidth. – rnrneverdies Commented Jul 5, 2015 at 8:19
  • @oneway Well, it's a matter of how narrowly one interprets the question. The question has a broader context of how to do the equivalent of endsWith; it has a narrower context of how to do that in one particular, suboptimal way. It's well within the scope of legitimate answers, IMHO, to suggest alternative, better approaches to solve the underlying question. A question is a duplicate if it asks the same basic question, especially when, as in this case, the question suggested as a duplicate does provide an answer giving a correct implementation based on substr. – user663031 Commented Jul 5, 2015 at 9:12
Add a ment  | 

6 Answers 6

Reset to default 6

EDIT

As @torazaburo said. The correct answer Is:

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

Because The string does end with a null string

ORIGINAL ANSWER

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

http://jsfiddle/tqsx0gLa/2/

From Comments: This code is setting up a logical parison using the && operator. The left side target.length > 0 should always return true with a valid target input. The left side is setting target equal to the substr starting at the point located by taking the str.length (the position at the far right of the str) and subtracting the target length (to arrive at the start point of our substring). There is no need for an end point input because the substring will run to the end of str.

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;
}
function end(str, target) {
  var start = str.length - target.length;

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

You can also try this code.

The substring method can take a negative value to work from the end of the string. The solution to your problem is very simple:

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

end("simple is better", "better"); // returns true

// which is the same as writing
"simple is better".substr(-6) === "better" // true again

Here you go:

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

The cleanest way.

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

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

本文标签: javascriptConfirm the Ending of an String (Varying Ending Length)Stack Overflow