Quantcast
Channel: Remove end of line characters from end of Java String - Stack Overflow
Browsing latest articles
Browse All 8 View Live

Answer by Cowan for Remove end of line characters from end of Java String

If you have Google's guava-librariesin your project (if not, you arguably should!) you'd do this with a CharMatcher: String result = CharMatcher.any("\r\n").trimTrailingFrom(input);

View Article



Answer by bezmax for Remove end of line characters from end of Java String

Well, everyone gave some way to do it with regex, so I'll give a fastest way possible instead: public String replace(String val) { for (int i=val.length()-1;i>=0;i--) { char c = val.charAt(i); if (c...

View Article

Answer by Stephen C for Remove end of line characters from end of Java String

The Apache Commons Lang StringUtils.stripEnd(String str, String stripChars) will do the trick; e.g. String trimmed = StringUtils.stripEnd(someString, "\n\r"); If you want to remove all whitespace at...

View Article

Answer by Jesper for Remove end of line characters from end of Java String

String text = "foo\r\nbar\r\nhello\r\nworld\r\n"; String result = text.replaceAll("[\r\n]+$", "");

View Article

Answer by polygenelubricants for Remove end of line characters from end of...

You can use s = s.replaceAll("[\r\n]+$", "");. This trims the \r and \n characters at the end of the string The regex is explained as follows: [\r\n] is a character class containing \r and \n + is...

View Article


Answer by Favonius for Remove end of line characters from end of Java String

"foo\r\nbar\r\nhello\r\nworld\r\n".replaceAll("\\s+$", "") or "foo\r\nbar\r\nhello\r\nworld\r\n".replaceAll("[\r\n]+$", "")

View Article

Answer by Richard Walton for Remove end of line characters from end of Java...

Wouldn't String.trim do the trick here? i.e you'd call the method .trim() on your string and it should return a copy of that string minus any leading or trailing whitespace.

View Article

Remove end of line characters from end of Java String

I have a string which I'd like to remove the end of line characters from the very end of the string only using Java "foo\r\nbar\r\nhello\r\nworld\r\n" which I'd like to become...

View Article

Browsing latest articles
Browse All 8 View Live




Latest Images