从Javastring中去除前导和尾随空格
可能重复:
修剪string的空白?
有没有一种方便的方法去除Javastring中的任何前导或尾随空格?
就像是:
String myString = " keep this "; String stripppedString = myString.strip(); System.out.println("no spaces:" + strippedString); 结果:
 no spaces:keep this 
  myString.replace(" ","")将取代keep和this之间的空格。 
谢谢
你可以尝试trim()方法。
 String newString = oldString.trim(); 
看看javadocs
 使用String#trim()方法或String allRemoved = myString.replaceAll("^\\s+|\\s+$", "")来修剪两端。 
对于左侧修剪:
 String leftRemoved = myString.replaceAll("^\\s+", ""); 
对于正确的修剪:
 String rightRemoved = myString.replaceAll("\\s+$", ""); 
从文档 :
 String.trim(); 
  trim()是你的select,但是如果你想使用replace方法 – 可能更灵活,你可以尝试下面的方法: 
 String stripppedString = myString.replaceAll("(^ )|( $)", "");