Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

instance method

String#bytesize

() -> ::Integer
bytesize → integer

Returns the count of bytes in self.

Note that the byte count may be different from the character count (returned by size):

Example
s = 'foo'
s.bytesize # => 3
s.size     # => 3
s = 'こんにちは'
s.bytesize # => 15
s.size     # => 5

Related: see Querying.

See also #bytes #byteslice #byterindex #bytesplice also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0