从Java字符串中去掉前导空格和尾随空格
是否有一种方便的方法可以从Java字符串中去掉任何前导或尾随空格 比如:从Java字符串中去掉前导空格和尾随空格,java,string,replace,Java,String,Replace,是否有一种方便的方法可以从Java字符串中去掉任何前导或尾随空格 比如: String myString = " keep this "; String stripppedString = myString.strip(); System.out.println("no spaces:" + strippedString); 结果: no spaces:keep this myString.replace(“,”)将替换keep和this之间的空格。您可以尝试trim()方法 从以下位置
String myString = " keep this ";
String stripppedString = myString.strip();
System.out.println("no spaces:" + strippedString);
结果:
no spaces:keep this
myString.replace(“,”)
将替换keep和this之间的空格。您可以尝试trim()方法
从以下位置查看:
trim()是您的选择,但是如果您想使用replace
方法,这可能更灵活,您可以尝试以下方法:
String stripppedString = myString.replaceAll("(^ )|( $)", "");
使用方法或String allRemoved=myString.replaceAll(“^\\s+\\s+$”,”)
修剪两端
对于左饰件:
String leftRemoved = myString.replaceAll("^\\s+", "");
对于右侧修剪:
String rightRemoved = myString.replaceAll("\\s+$", "");
对于Java-11和更高版本,您可以使用API返回一个值为该字符串的字符串,并删除所有前导和尾随空格。同样的javadoc如下所示:
/**
* Returns a string whose value is this string, with all leading
* and trailing {@link Character#isWhitespace(int) white space}
* removed.
* <p>
* If this {@code String} object represents an empty string,
* or if all code points in this string are
* {@link Character#isWhitespace(int) white space}, then an empty string
* is returned.
* <p>
* Otherwise, returns a substring of this string beginning with the first
* code point that is not a {@link Character#isWhitespace(int) white space}
* up to and including the last code point that is not a
* {@link Character#isWhitespace(int) white space}.
* <p>
* This method may be used to strip
* {@link Character#isWhitespace(int) white space} from
* the beginning and end of a string.
*
* @return a string whose value is this string, with all leading
* and trailing white space removed
*
* @see Character#isWhitespace(int)
*
* @since 11
*/
public String strip()
要修剪特定字符,可以使用:
String s = s.replaceAll("^(,|\\s)*|(,|\\s)*$", "")
这里将去掉前导和尾随的空格和逗号s。strip()可以从java 11开始使用
s、 trim()您可以使用。这很不幸,但这意味着这里的答案对人们很有用。我只是出于这个原因投了赞成票。虽然这可能是一个重复的问题,但这是一个更好的问题。如果有什么区别的话,另一个应该和这个一样接近。我切换了副本,因为这个问答有更多的视图和收藏夹,另一个问答实际上是一个调试问题。用-
字符串回答了这个问题。strip
可以告诉你字符串中有多少前导/尾随空格。它能代替什么?可能是空格和换行符?我在寻找一种解决方案,只删除尾随空格而不删除前导空格。我用了:str.replaceAll(“\\s*$”,“”)谢谢!PS:根据来自-的注释将答案迁移到这里,另请参见。作为Java11的String.strip()的向后兼容替代品。我还没有时间去探究这些细微的差别。
/**
* Returns a string whose value is this string, with all leading
* and trailing {@link Character#isWhitespace(int) white space}
* removed.
* <p>
* If this {@code String} object represents an empty string,
* or if all code points in this string are
* {@link Character#isWhitespace(int) white space}, then an empty string
* is returned.
* <p>
* Otherwise, returns a substring of this string beginning with the first
* code point that is not a {@link Character#isWhitespace(int) white space}
* up to and including the last code point that is not a
* {@link Character#isWhitespace(int) white space}.
* <p>
* This method may be used to strip
* {@link Character#isWhitespace(int) white space} from
* the beginning and end of a string.
*
* @return a string whose value is this string, with all leading
* and trailing white space removed
*
* @see Character#isWhitespace(int)
*
* @since 11
*/
public String strip()
System.out.println(" leading".strip()); // prints "leading"
System.out.println("trailing ".strip()); // prints "trailing"
System.out.println(" keep this ".strip()); // prints "keep this"
String s = s.replaceAll("^(,|\\s)*|(,|\\s)*$", "")