Java Regex删除新行,但保留空格。 [英] Java Regex remove new lines, but keep spaces.

查看:99
本文介绍了Java Regex删除新行,但保留空格。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于字符串\ nabc \\\
1 2 3 \ nxyz
我需要它成为abc 1 2 3 xyz

For the string " \n a b c \n 1 2 3 \n x y z " I need it to become "a b c 1 2 3 x y z".

使用此正则表达式str.replaceAll(((\ | | \ n),);我可以得到abc123xyz,但是如何在中间获得空格。

Using this regex str.replaceAll("(\s|\n)", ""); I can get "abc123xyz", but how can I get spaces in between.

推荐答案

你不必使用正则表达式;您可以使用 trim() replaceAll()代替。

You don't have to use regex; you can use trim() and replaceAll() instead.

 String str = " \n a b c \n 1 2 3 \n x y z ";
 str = str.trim().replaceAll("\n ", "");

这将为您提供您正在寻找的字符串。

This will give you the string that you're looking for.

这篇关于Java Regex删除新行,但保留空格。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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