-2

In Javascript, I have a generated string containing multiple lines, all ending in a newline, for example: "line1\nline2\nline3\n"

What is the best way to cut off the ending newline such that I get "line1\nline2\nline3" ?

For safety, it would be preferrable if the last character is only cut off if it actually is a newline. So if my input is "field1=5\nfield2=abc\nfield3= some string \n" I want to remove the last newline but keep the two spaces.

Axel Podehl
  • 4,034
  • 29
  • 41
  • 2
    [`trim`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trim)? – jonrsharpe Jan 10 '20 at 07:51
  • trim() is a good idea but there's a slim possibility that my list line contains spaces at the end, let me add another example – Axel Podehl Jan 10 '20 at 07:55
  • maybe, but none of these solutions are 'simple' - I was hoping there is something already built-in like Java StringUtils.chomp() – Axel Podehl Jan 10 '20 at 08:01
  • By all means propose that via TC39, then add it as an answer to the dupe. But as you can see by the answers below, those are the answers. – jonrsharpe Jan 10 '20 at 08:03
  • Ok, I see, so .replace(/\n+$/, ''); is the answer then, thanks. – Axel Podehl Jan 10 '20 at 08:08
  • You could also put a bounty on the dupe, I believe wanting to get newer answers (e.g. utilising new language features) is one of the canned reasons. – jonrsharpe Jan 10 '20 at 08:14
  • Well, I don't really think it's an exact duplicate because that other question is about trim() (from both sides), while mine is only about the end - chomp(). – Axel Podehl Jan 10 '20 at 08:17
  • https://stackoverflow.com/questions/12248854/javascript-remove-last-character-if-a-colon, https://stackoverflow.com/questions/952924/javascript-chop-slice-trim-off-last-character-in-string, https://stackoverflow.com/questions/17720264/remove-last-comma-from-a-string, https://stackoverflow.com/questions/3084708/remove-the-last-n-from-a-textarea, https://stackoverflow.com/questions/34214180/how-to-remove-the-line-break-at-the-end-of-a-string - with stuff like this, there is *no way* you're going to be the first person asking it. – jonrsharpe Jan 10 '20 at 08:20
  • Ok, thanks for finding these answers. But asking a simple question on stack exchanged is punished with -4 points? I don’t get it. – Axel Podehl Jan 10 '20 at 13:20
  • The reason for downvotes on any question is in the tooltip: *"This question does not show any research effort; it is unclear or not useful"*. – jonrsharpe Jan 10 '20 at 17:58

2 Answers2

1

There are lots of general-purpose solutions out there, but JavaScript itself doesn't really contain a built-in specifically for removing a particular character. The replace() method combined with a regular expression, however, still does the trick just fine:

var string = "field1=5\nfield2=abc\nfield3=  some string  \n";
var trimmed = string.replace(/\n+$/, '');
B. Fleming
  • 7,170
  • 1
  • 18
  • 36
0

Try:

var str = "line1\nline2\nline3\n"
if (str[str.length - 1] === '\n') str = str.substring(0, str.length - 1);
console.log(str);
Marco
  • 246
  • 1
  • 9
  • 1
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](/review/low-quality-posts/25057481) – cloned Jan 10 '20 at 08:38
  • I'm sorry... I might have misunderstood the question... I think he asked how to delete the last /n in a string (so only the last character, if that is a /n). I was not making a critique or requesting a clarification... I was attempting to provide and actual answer... If I didn't, I apologize... – Marco Jan 10 '20 at 12:28