Logo

Programming-Idioms

Assign to n the number of bytes in the string s.

This can be different from the number of characters. If n includes more bytes than the characters per se (trailing zero, length field, etc.) then explain it. One byte is 8 bits.
New implementation

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
import 'dart:convert';
var n = utf8.encode(s).length;
n := len(s)
$n = strlen($s);
n := length(s);
use v5.10;
use charnames qw( :full );
use Encode;
my $text = "\N{LATIN SMALL LETTER E WITH ACUTE}";
say length $text;  # prints 1

my $utf8 = Encode::encode('UTF-8', $text);
say length $utf8;  # prints 2
n = len(s.encode('utf8'))
n = s.bytesize
let n = s.len();