在Java中的新行之前删除空间 [英] removing space before new line in java

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

问题描述

我在字符串中的新行之前有一个空格,无法将其删除(在Java中). 我已经尝试了以下方法,但没有任何效果:

i have a space before a new line in a string and cant remove it (in java). I have tried the following but nothing works:

strToFix = strToFix.trim();
strToFix = strToFix.replace(" \n", "");
strToFix = strToFix.replaceAll("\\s\\n", "");

推荐答案

myString.replaceAll("[ \t]+(\r\n?|\n)", "$1");

replaceAll将正则表达式作为参数. [ \t]与一个或多个空格或制表符匹配. (\r\n?|\n)匹配换行符,并将结果放入$1.

replaceAll takes a regular expression as an argument. The [ \t] matches one or more spaces or tabs. The (\r\n?|\n) matches a newline and puts the result in $1.

这篇关于在Java中的新行之前删除空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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