最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Remove Everything After Certain Character Within String Using JavaScript - Stack Overflow

matteradmin9PV0评论

EDIT: My code does work - I just had a typo when I was console.logging. It uses the same technique already found in the answer here.

I have a function that should remove everything after the ma in the string:

function shortenToDate(longDate) {

  let newDate = longDate.substring(0, longDate.indexOf(","));

  return newDate;

}

^ You just need to take a chunk out of the string from the 0 index to the indexOf() the first instance of whatever character you wish to remove everything after.

I had also tried:

function shortenToDate(longDate) {

  return longDate.substring(longDate.indexOf(0, ","));

}

console.log(shortenToDate(shortenToDate("Friday May 2, 9am")));

Which didn't have any effect. It just returned Friday May 2, 9am.

EDIT: My code does work - I just had a typo when I was console.logging. It uses the same technique already found in the answer here.

I have a function that should remove everything after the ma in the string:

function shortenToDate(longDate) {

  let newDate = longDate.substring(0, longDate.indexOf(","));

  return newDate;

}

^ You just need to take a chunk out of the string from the 0 index to the indexOf() the first instance of whatever character you wish to remove everything after.

I had also tried:

function shortenToDate(longDate) {

  return longDate.substring(longDate.indexOf(0, ","));

}

console.log(shortenToDate(shortenToDate("Friday May 2, 9am")));

Which didn't have any effect. It just returned Friday May 2, 9am.

Share Improve this question edited Apr 15, 2019 at 16:18 HappyHands31 asked Apr 15, 2019 at 2:30 HappyHands31HappyHands31 4,11119 gold badges66 silver badges117 bronze badges 6
  • 1 Regarding your second snippet: "And that didn't return anything"... it clearly does: jsfiddle/wxprm41z – Gerardo Furtado Commented Apr 15, 2019 at 2:33
  • 2 Here is an image to show you (answering to your now deleted ment): imgur./UZTF6Be. – Gerardo Furtado Commented Apr 15, 2019 at 2:39
  • So then my second guess was actually working. I don't know why it wasn't logging anything to the console before. – HappyHands31 Commented Apr 15, 2019 at 2:42
  • Yes, it was working. So, at the end, your question was unnecessary! What strikes me the most is that 3 high-rep users saw your question, saw that you had a working code (I hope so...) but, instead of voting to close and leave you a ment, decided to write an answer. – Gerardo Furtado Commented Apr 15, 2019 at 3:14
  • 1 @GerardoFurtado I'm going to go ahead and delete this question. The question that I linked already has the correct answer with (0, longDate.indexOf(",")). – HappyHands31 Commented Apr 15, 2019 at 14:34
 |  Show 1 more ment

3 Answers 3

Reset to default 4

You can simply use split and take the 0th index

const shortenToDate = longDate => longDate.split(',',1)[0];
console.log(shortenToDate("Friday May 2, 9am"))

Problems

In the first snippet you're using

longDate.substring(longDate.indexOf(","), longDate.length -1);

but you want from 0th index

const  shortenToDate = longDate => longDate.substring(0,longDate.indexOf(","));

console.log(shortenToDate("Friday May 2, 9am"))

How about that with String​.prototype​.split() and Array.prototype​.shift()?

function shortenToDate(longDate) {
  let newDate = longDate.split(',');
  return newDate.shift();
}

console.log(shortenToDate("Friday May 2, 9am"))

It would probably be easier to use a regular expression - match a ma, followed by any characters, and replace with the empty string:

const shortenToDate = longDate => longDate.replace(/,.*/, '');
console.log(shortenToDate("Friday May 2, 9am"))

Post a comment

comment list (0)

  1. No comments so far