Java Regex - 减少字符串中的空格 [英] Java Regex - reduce spaces in a string

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

问题描述

我没有时间了解正则表达式,我需要快速回答。平台是Java。

I don't have time to get my head around regex and I need a quick answer. Platform is Java.

我需要字符串

"Some text   with  spaces"

...转换为

"Some text with spaces"

即2个或更多连续的空格要改为1个空格。

i.e., 2 or more consecutive spaces to be changed to 1 space.

推荐答案

String a = "Some    text  with   spaces";
String b = a.replaceAll("\\s+", " ");
assert b.equals("Some text with spaces");

这篇关于Java Regex - 减少字符串中的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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