Created
October 19, 2010 15:17
-
-
Save fumokmm/634367 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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