arjun ajjarapu
arjun ajjarapu

Reputation: 91

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.

Upvotes: 2

Views: 7010

Answers (2)

Om Prakash
Om Prakash

Reputation: 853

You may use this too.

 new File("C:/org.txt").text.replaceAll("[\r\n]+","")

Upvotes: 3

tim_yates
tim_yates

Reputation: 171084

Get the lines and join them

new File('C:/org.txt').readLines().join()

Upvotes: 4

Related Questions