如何从Java中的字符串中删除前导和尾随空格?
我想从字符串中删除前导和尾随空格:
String s = " Hello World ";
我希望结果如下:
s == "Hello world";
I want to remove the leading and trailing whitespace from string:
String s = " Hello World ";
I want the result to be like:
s == "Hello world";
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
请参阅 String#trim( )
没有任何内部方法,使用正则表达式,如
or
或仅使用纯形式的模式
see String#trim()
Without any internal method, use regex like
or
or just use pattern in pure form
我不喜欢使用正则表达式来解决琐碎的问题。这将是一个简单的选择:
I prefer not to use regular expressions for trivial problems. This would be a simple option:
String.trim() 回答了问题,但对我来说不是一个选择。
如此处所述:
我正在使用日语,我们有全角字符,就像这样,全角空格不会被 String.trim() 修剪。
因此,我创建了一个函数,就像 xehpuk 的代码片段一样,使用 Character.isWhitespace()。
然而,这个版本没有使用 StringBuilder,并且不是删除字符,而是找到从原始字符串中取出修剪子字符串所需的 2 个索引。
String.trim() answers the question but was not an option for me.
As stated here :
I am working with Japanese where we have full-width characters Like this, the full-width space would not be trimmed by String.trim().
I therefore made a function which, like xehpuk's snippet, use Character.isWhitespace().
However, this version is not using a StringBuilder and instead of deleting characters, finds the 2 indexes it needs to take a trimmed substring out of the original String.
从 Java 11 String 类开始,有 strip() 方法,用于返回值为该字符串的字符串,并删除所有前导和尾随空格。引入这是为了克服修剪方法的问题。
文档: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#strip()
示例:
还有两个比较有用Java 11+ 字符串中的方法类:
stripLeading() :返回一个字符串,其值为该字符串,
删除所有前导空白。
stripTrailing() :返回一个字符串,其值为该字符串,
删除所有尾随空格。
Since Java 11 String class has strip() method which is used to returns a string whose value is this string, with all leading and trailing white space removed. This is introduced to overcome the problem of trim method.
Docs: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#strip()
Example:
There are two more useful methods in Java 11+ String class:
stripLeading() : Returns a string whose value is this string,
with all leading white space removed.
stripTrailing() : Returns a string whose value is this string,
with all trailing white space removed.
使用 String 类的修剪方法。它将删除所有前导和尾随空格。
http://docs.oracle.com/javase /1.5.0/docs/api/java/lang/String.html
Use the String class trim method. It will remove all leading and trailing whitespace.
http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html
有关详细信息 请参阅此
For more information See This
只需使用trim()。它仅消除字符串开头和结尾多余的空格。
String fav = "我喜欢苹果";
输出:
我喜欢苹果//字符串开头和结尾没有多余的空格
Simply use trim(). It only eliminate the start and end excess white spaces of a string.
String fav = " I like apple ";
Output:
I like apple //no extra space at start and end of the string
更多信息:
http://docs.oracle .com/javase/7/docs/api/java/lang/String.html#trim()
为什么不想使用预定义的方法?他们通常是最有效率的。
More info:
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#trim()
Why do you not want to use predefined methods? They are usually most efficient.
请参阅String#trim()< /a> 方法
See String#trim() method
虽然如果您想避免使用正则表达式,@xehpuk 的方法很好,但它的时间复杂度为 O(n^2)。以下解决方案也避免了正则表达式,但时间复杂度为 O(n):
这对字符串开头和结尾处的尾随空格数进行计数,直到从空格计数获得的“左”和“右”索引满足,或者两个索引满足已达到非空白字符。之后,我们要么返回使用空格计数获得的子字符串,如果结果是空格,则返回空字符串(需要考虑具有奇数个字符的全空格字符串)。
While @xehpuk's method is good if you want to avoid using regex, but it has O(n^2) time complexity. The following solution also avoids regex, but is O(n):
This counts the number of trailing whitespaces in the beginning and end of the string, until either the "left" and "right" indices obtained from whitespace counts meet, or both indices have reached a non-whitespace character. Afterwards, we either return the substring obtained using the whitespace counts, or the empty string if the result is a whitespace (needed to account for all-whitespace strings with odd number of characters).
没有
trim
方法就很简单:It's simple without
trim
method: