Robert Strauch
Robert Strauch

Reputation: 12896

Removing first line from string in Groovy

With Groovy I need to read an XML file and remove the first line containing the XML declaration.

Source content

<?xml version="1.0" encoding="UTF-8"?>
<myxmldoc>
  content
</myxmldoc>

Target content

<myxmldoc>
  content
</myxmldoc>

My code so far... Reading all lines except the first line from the file into a list. Then, building a new string by adding each line to the next.

def soapBodyList = new File(inputFilename).readLines()
soapBodyList = soapBodyList[1..soapBodyList.size-1]

def soapBody = ""
soapBodyList.each {
    soapBody += it
}

return soapBody

However I feel that there must be a simpler way to just remove the first line abnd have the result as a string.

Upvotes: 2

Views: 5352

Answers (1)

Erich Kitzmueller
Erich Kitzmueller

Reputation: 36987

def soapBodyList = new File(inputFilename).readLines()
return soapBodyList[1..soapBodyList.size-1].join("")

should do the trick

Upvotes: 5

Related Questions