158

我有一个字符串,其中包含一些文本,后跟一个空行。保留文本部分的最佳方法是什么,但从末尾删除空格换行符?

4

12 回答 12

335

使用String.trim()方法去除字符串开头和结尾的空格(空格、换行符等)。

String trimmedString = myString.trim();
于 2011-09-17T11:19:39.953 回答
26
String.replaceAll("[\n\r]", "");
于 2011-09-17T11:51:41.760 回答
14

This Java code does exactly what is asked in the title of the question, that is "remove newlines from beginning and end of a string-java":

String.replaceAll("^[\n\r]", "").replaceAll("[\n\r]$", "")

Remove newlines only from the end of the line:

String.replaceAll("[\n\r]$", "")

Remove newlines only from the beginning of the line:

String.replaceAll("^[\n\r]", "")
于 2020-02-24T15:36:15.343 回答
9
于 2019-05-20T21:33:23.570 回答
5

如果您的字符串可能是null,请考虑使用StringUtils.trim()- 的空安全版本String.trim()

于 2017-10-25T23:13:42.270 回答
3

If you only want to remove line breaks (not spaces, tabs) at the beginning and end of a String (not inbetween), then you can use this approach:

Use a regular expressions to remove carriage returns (\\r) and line feeds (\\n) from the beginning (^) and ending ($) of a string:

 s = s.replaceAll("(^[\\r\\n]+|[\\r\\n]+$)", "")

Complete Example:

public class RemoveLineBreaks {
    public static void main(String[] args) {
        var s = "\nHello world\nHello everyone\n";
        System.out.println("before: >"+s+"<");
        s = s.replaceAll("(^[\\r\\n]+|[\\r\\n]+$)", "");
        System.out.println("after: >"+s+"<");
    }
}

It outputs:

before: >
Hello world
Hello everyone
<
after: >Hello world
Hello everyone<
于 2020-07-31T12:25:47.050 回答
2

我也将为此添加一个答案,因为虽然我有同样的问题,但提供的答案还不够。经过一番思考,我意识到这可以通过正则表达式轻松完成。

从开头删除换行符:

// Trim left
String[] a = "\n\nfrom the beginning\n\n".split("^\\n+", 2);

System.out.println("-" + (a.length > 1 ? a[1] : a[0]) + "-");

和字符串的结尾:

// Trim right
String z = "\n\nfrom the end\n\n";

System.out.println("-" + z.split("\\n+$", 2)[0] + "-");

我确信这不是修剪字符串的最高效的方法。但它似乎是内联此类操作的最干净和最简单的方法。

请注意,可以使用相同的方法从任一端修剪字符的任何变体和组合,因为它是一个简单的正则表达式。

于 2017-03-06T07:14:14.807 回答
2

Try this

function replaceNewLine(str) { 
  return str.replace(/[\n\r]/g, "");
}
于 2020-07-31T15:17:43.857 回答
1
String trimStartEnd = "\n TestString1 linebreak1\nlinebreak2\nlinebreak3\n TestString2 \n";
System.out.println("Original String : [" + trimStartEnd + "]");
System.out.println("-----------------------------");
System.out.println("Result String : [" + trimStartEnd.replaceAll("^(\\r\\n|[\\n\\x0B\\x0C\\r\\u0085\\u2028\\u2029])|(\\r\\n|[\\n\\x0B\\x0C\\r\\u0085\\u2028\\u2029])$", "") + "]");
  1. 字符串的开头 = ^ ,
  2. 字符串结束 = $ ,
  3. 正则表达式组合 = | ,
  4. 换行 = \r\n|[\n\x0B\x0C\r\u0085\u2028\u2029]
于 2018-07-18T06:26:32.533 回答
0

Another elegant solution.

String myString = "\nLogbasex\n";
myString = org.apache.commons.lang3.StringUtils.strip(myString, "\n");
于 2020-10-08T03:27:46.180 回答
0

For anyone else looking for answer to the question when dealing with different linebreaks:

string.replaceAll("(\n|\r|\r\n)$", ""); // Java 7
string.replaceAll("\\R$", "");          // Java 8

This should remove exactly the last line break and preserve all other whitespace from string and work with Unix (\n), Windows (\r\n) and old Mac (\r) line breaks: https://stackoverflow.com/a/20056634, https://stackoverflow.com/a/49791415. "\\R" is matcher introduced in Java 8 in Pattern class: https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html

This passes these tests:

// Windows:
value = "\r\n test \r\n value \r\n";
assertEquals("\r\n test \r\n value ", value.replaceAll("\\R$", ""));

// Unix:
value = "\n test \n value \n";
assertEquals("\n test \n value ", value.replaceAll("\\R$", ""));

// Old Mac:
value = "\r test \r value \r";
assertEquals("\r test \r value ", value.replaceAll("\\R$", ""));
于 2020-10-28T20:58:39.567 回答
-4
String text = readFileAsString("textfile.txt");
text = text.replace("\n", "").replace("\r", "");
于 2014-07-20T06:44:06.103 回答