简体   繁体   中英

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?

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. 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]+","")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM