java字符串中如何将多个空格替换为一个空格呢?
下文笔者讲述将字符串中多个空格转换为一个空格的方法及示例分享,如下所示
多个空格转一个空格的实现思路: 使用正则表达式即可实现多个空格转一个空格 如: str=str.replaceAll("\\s+"," ")例
//字符串中多个空格替换成一个空格 public class TestClass { public static void main(String[] args) { String w= ""; String str="test information"; Pattern p = Pattern.compile("\\s+"); Matcher m = p.matcher(str); w= m.replaceAll(" "); System.out.println(w); } } 或 str=str.replaceAll("\\s+"," ");
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。