Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

Ruby | String | bytesize

概要

String#bytesize -> Integer

詳細

文字列のバイト長を整数で返却

サンプルコード
require 'tbpgr_utils'

bulk_puts_eval binding, <<-EOS
"hoge".bytesize
"ほげ".bytesize
"hoげ".bytesize
"hoげ".encoding
"hoげ".encode("Shift_JIS").bytesize
EOS

__END__
下記はTbpgrUtils gemの機能
bulk_puts_eval

https://rubygems.org/gems/tbpgr_utils
https://github.com/tbpgr/tbpgr_utils

出力

"hoge".bytesize                     # => 4
"ほげ".bytesize                     # => 6
"hoげ".bytesize                     # => 5
"hoげ".encoding                     # =>   #<Encoding:UTF-8>
"hoげ".encode("Shift_JIS").bytesize # => 4