Skip to content

Instantly share code, notes, and snippets.

@toby55kij
Created June 24, 2011 12:48
Show Gist options
  • Select an option

  • Save toby55kij/1044702 to your computer and use it in GitHub Desktop.

Select an option

Save toby55kij/1044702 to your computer and use it in GitHub Desktop.
「Groovyで文字列を最大nバイトに切り詰める」を書き換えてみる
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