Skip to content

Instantly share code, notes, and snippets.

@aeg
Created May 11, 2013 17:53
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 aeg/5560797 to your computer and use it in GitHub Desktop.
Save aeg/5560797 to your computer and use it in GitHub Desktop.
文字列の末尾から何文字目までを取り出す
// Case #1
// 文字列の末尾から何文字目までを取り出す(BasicのRight関数の動き)
def rightStr(String string, int cutLength) {
string.substring(string.length() - cutLength)
}
assert rightStr("12345678",3) == "678"
// Case #2
// 文字列の末尾から何文字目までを取り出す。Stringにメソッド追加。
String.metaClass.right = { cutLength ->
delegate.substring(delegate.length()- cutLength)
}
assert "abcdefg".right(4) == "defg"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment