liuxu
liuxu

Reputation: 37

Why groovy println function prints extra empty line?

I noticed the println function in groovy will print extra empty line:

  string myline=extractedLine
  println 'myline:'+myline

The expected output is '101110' but the actual output shows 2 lines been printed out first line is an empty line and second line is '101110'. May I know how to remove the empty line?

I tried to use print instead of println, but still the same result.

Upvotes: 0

Views: 129

Answers (1)

ycr
ycr

Reputation: 14574

I'm assuming the value in myline itself has a newline character(line feeder or a carriage return), hence try something like the one below.

print "myline: " + myline.replaceAll('\n|\r', '')

As @tim_yates mentioned if you just have newlines at the beginning or at the end of the string you can simply call trim() as well.

print "myline: " + myline.trim()

Upvotes: 1

Related Questions