I have a method that returns a String with multiple lines. I want to parse the String and get the first word of each line.
Method getText()
returns:
Lorem ipsum dolor
sit amet odio
magnis vitae iaculis
I want to get only
Lorem
sit
magnis
My current code is
def projectString = getText()
def projects = projectString.substring(0, projectString.indexOf(' '))
Of course that only gets the first word of the first line. I could use a while loop on the string based on new lines and get the first word using the substring
method above, but I have a feeling that Groovy has a groovier way of doing this.
Initially I was thinking about using a pipe on the method call result so something like
def projects = getText() | sh "awk '{print $1}'"
But I couldn't get that to work.