Created
June 24, 2011 12:48
-
-
Save toby55kij/1044702 to your computer and use it in GitHub Desktop.
「Groovyで文字列を最大nバイトに切り詰める」を書き換えてみる
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
| def takeNbytes(str, n) { | |
| buf = '' | |
| for (s in str) { | |
| if ( buf.bytes.size() + s.bytes.size() > n) break | |
| buf += s | |
| } | |
| buf | |
| } | |
| println takeNbytes("あいうえお", 2) | |
| println takeNbytes("あいうえお", 3) | |
| println takeNbytes("あいうえお", 4) | |
| println takeNbytes("あいうえお", 5) | |
| println takeNbytes("あいうえお", 6) | |
| println takeNbytes("abcdef", 5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment