[英]Groovy script remove newline from StringBuffer
Thanks for taking the time to look into this. 感谢您抽出宝贵的时间对此进行研究。
How do I get rid of all newline characters from a StringBuffer? 如何摆脱StringBuffer中的所有换行符?
The file that i'm reading in looks something like this - 我正在读取的文件看起来像这样-
USER|SLACK|TRELLO|BINARY|YO!##!
1234|Joe|||10001!##!
3212|Test1|||10001!##!
2213|Tin Man||24|10001!##!
I tried to use replaceAll(pattern,closure) like shown below. 我试图使用replaceAll(pattern,closure)如下所示。 But somehow cant quite get my head around it.
但是不知何故我无法绕开它。
package carriageReturnRemove
class asdf {
static void main(def args){
String str = new File('C:/org.txt').getText()
StringBuilder sb = new StringBuilder(str)
sb = sb.replaceAll(~'/n','')
}
} }
The output basically needs to look like this - 输出基本上需要看起来像这样-
USER|SLACK|TRELLO|BINARY|YO!##!1234|Joe|||10001!##!3212|Test1|||10001!##!2213|Tin Man||24|10001!##!
Where am I going wrong? 我要去哪里错了? Any help or pointers are greatly appreciated.
任何帮助或指针,我们将不胜感激。
Cheers. 干杯。
排队并加入他们
new File('C:/org.txt').readLines().join()
您也可以使用它。
new File("C:/org.txt").text.replaceAll("[\r\n]+","")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.