从 Java 字符串中去除前导和尾随空格 [英] Strip Leading and Trailing Spaces From Java String

查看:30
本文介绍了从 Java 字符串中去除前导和尾随空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方便的方法可以从 Java 字符串中去除任何前导或尾随空格?

Is there a convenience method to strip any leading or trailing spaces from a Java String?

类似于:

String myString = "  keep this  ";
String stripppedString = myString.strip();
System.out.println("no spaces:" + strippedString);

结果:

no spaces:keep this

myString.replace(" ","") 将替换 keep 和 this 之间的空格.

myString.replace(" ","") would replace the space between keep and this.

推荐答案

可以试试trim()方法.

You can try the trim() method.

String newString = oldString.trim();

看看javadocs

这篇关于从 Java 字符串中去除前导和尾随空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆