是否有一种方便的方法可以从 Java String 中去除任何前导或尾随空格?
就像是:
String myString = " keep this ";
String stripppedString = myString.strip();
System.out.println("no spaces:" + strippedString);
结果:
no spaces:keep this
myString.replace(" ","")
将替换 keep 和 this 之间的空格。
原文由 Tyler DeWitt 发布,翻译遵循 CC BY-SA 4.0 许可协议
您可以尝试 trim() 方法。
看看 javadocs