如何从字符串的开头和结尾删除换行符?
我有一个字符串,其中包含一些文本,后跟一个空行。保留文本部分但删除末尾的空白换行符的最佳方法是什么?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我有一个字符串,其中包含一些文本,后跟一个空行。保留文本部分但删除末尾的空白换行符的最佳方法是什么?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(12)
使用
String.trim()
方法从字符串的开头和结尾删除空格(空格、换行符等)。Use
String.trim()
method to get rid of whitespaces (spaces, new lines etc.) from the beginning and end of the string.这段Java代码完全按照问题标题中的要求进行操作,即“从字符串java的开头和结尾删除换行符”:
仅从行尾删除换行符:
仅从行首删除换行符:
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":
Remove newlines only from the end of the line:
Remove newlines only from the beginning of the line:
tl;dr
String::strip...
旧的
String::trim
方法有一个 空白的奇怪定义。正如此处所述,Java 11 添加了新的
strip...
方法到String
类。它们使用更符合 Unicode 的空白定义。请参阅类 JavaDoc 中此定义的规则字符::isWhitespace
。示例代码。
或者您可以删除 只是前导 或 只是尾随 空格。
您没有确切提及代码点组成换行符。我想您的换行符可能包含在
strip
所针对的代码点列表中:tl;dr
String::strip…
The old
String::trim
method has a strange definition of whitespace.As discussed here, Java 11 adds new
strip…
methods to theString
class. These use a more Unicode-savvy definition of whitespace. See the rules of this definition in the class JavaDoc forCharacter::isWhitespace
.Example code.
Or you can strip just the leading or just the trailing whitespace.
You do not mention exactly what code point(s) make up your newlines. I imagine your newline is likely included in this list of code points targeted by
strip
:如果您的字符串可能为
null
,请考虑使用StringUtils.trim()
-String.trim()
。If your string is potentially
null
, consider usingStringUtils.trim()
- the null-safe version ofString.trim()
.如果您只想删除字符串开头和结尾(而不是中间)的换行符(不是空格、制表符),那么您可以使用以下方法
:用于从开头 (
^
) 和结尾 (< code>$) 的字符串:完整示例:
它输出:
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:Complete Example:
It outputs:
我也将对此添加一个答案,因为虽然我有同样的问题,但提供的答案还不够。经过一番思考,我意识到使用正则表达式可以非常轻松地完成此操作。
要从
字符串的开头和结尾删除换行符:
我确信这不是修剪字符串的最高效的方法。但它似乎确实是内联此类操作的最干净、最简单的方法。
请注意,可以使用相同的方法来修剪两端字符的任何变体和组合,因为它是一个简单的正则表达式。
I'm going to add an answer to this as well because, while I had the same question, the provided answer did not suffice. Given some thought, I realized that this can be done very easily with a regular expression.
To remove newlines from the beginning:
and end of a string:
I'm certain that this is not the most performance efficient way of trimming a string. But it does appear to be the cleanest and simplest way to inline such an operation.
Note that the same method can be done to trim any variation and combination of characters from either end as it's a simple regex.
试试这个
Try this
另一个优雅的解决方案。
Another elegant solution.
对于在处理不同换行符时寻找问题答案的其他人:
这应该准确删除最后一个换行符并保留字符串中的所有其他空白,并适用于 Unix (\n)、Windows (\r\n) 和旧 Mac ( \r) 换行符:https://stackoverflow.com/a/20056634,https://stackoverflow.com/a/49791415。
"\\R"
是 Java 8 在 Pattern 类中引入的匹配器: https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html这通过了这些测试:
For anyone else looking for answer to the question when dealing with different linebreaks:
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.htmlThis passes these tests: