A regex-based solution that does not require converting the input string to a list:
def data = '''\
finally interesting text'''
assert data.replaceAll(/#.*\n/, '') == 'finally interesting text'
If you need to split the input lines into lines, you can still use regular expressions if you want using the method Collection#grep:
assert data.split('\n').grep(~/[^#].*/) == ['finally interesting text']
PS: Regexps FTW! = P
source
share