Created
May 11, 2013 17:51
-
-
Save aeg/5560790 to your computer and use it in GitHub Desktop.
文字列の先頭から何文字目までを取り出す
This file contains hidden or 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
// Case #1 | |
// 文字列の先頭から何文字目までを取り出す(BasicのLEFT関数の動き) | |
def leftStr(String string, int length) { | |
string.substring(0, length) | |
} | |
assert leftStr("12345678",3) == "123" | |
// Case #2 | |
// 文字列の先頭から何文字目までを取り出す.Stringへのメソッド追加方式。 | |
String.metaClass.left = { cutLength-> | |
delegate.substring(0,cutLength) | |
} | |
assert "12345678".left(4) == "1234" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment