Skip to content

Instantly share code, notes, and snippets.

@fumokmm
Created October 19, 2010 15:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fumokmm/634367 to your computer and use it in GitHub Desktop.
Save fumokmm/634367 to your computer and use it in GitHub Desktop.
// Stringを拡張
String.metaClass.strip = { first ->
def reg = (first ?: '|').with{ [/^.*\${it}.*$/, /^.*\${it}/] }
delegate.readLines()
.grep(~reg[0])*.replaceFirst(reg[1], '')
.join(System.properties['line.separator'])
}
// 検査用文字列
def str = '''
| >.a
| > .b
| > .c
'''
// デフォルトの '|' でストリップしてみる
assert str.strip() == '''\
>.a
> .b
> .c'''
// お次は '>' でストリップ
assert str.strip('>') == '''\
.a
.b
.c'''
// 最後は '.' だ
assert str.strip('.') == '''\
a
b
c'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment