Groovy web console

subscribe to the feed Subscribe
to this
Lines in Strings (via #groovywebconsole)
tweet this snippet Tweet

Lines in Strings

Published 10 years ago by mrhaki with tags string
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
def multiline = '''\
Groovy is closely related to Java,
so it is quite easy to make a transition.

// eachLine takes a closure with one argument, that
// contains the complete line.
multiline.eachLine { 
    if (it =~ /Groovy/) {
        println it  // Output: Groovy is closely related to Java,

// or eachLine has a closure with two argument, the current line
// and the line count.
multiline.eachLine { line, count -> 
    if (count == 0) {
        println "line $count: $line"  // Output: line 0: Groovy is closely related to Java,

def platformLinefeeds = multiline.denormalize()
def linefeeds = multiline.normalize()

// Read all lines and convert to list.
def list = multiline.readLines()
assert list instanceof ArrayList
assert 2 == list.size()
assert 'Groovy is closely related to Java,' == list[0]

def records = """\

// splitEachLine will split each line with the specified
// separator. The closure has one argument, the list of 
// elements separated by the separator.
records.splitEachLine('\t') { items ->
    println items[0] + " likes " + items[1]
// Output:
// mrhaki likes Groovy
// hubert likes Java