This language bar is your friend. Select your favorite languages!

Idiom #169 String length

Assign to integer n the number of characters of string s.

This can be different from the number of bytes of s.

Illustration
(def n (.codePointCount s 0 (count s)))
s : String := "Hello World";
n : Integer := s'Length;
#include <stdlib.h>
size_t	ft_strlen(const char *str)
{
	int	count;

	count = 0;
	while (str[count])
		count++;
	return (count);
}
IDENTIFICATION DIVISION.
PROGRAM-ID. length.
PROCEDURE DIVISION.
   MOVE FUNCTION LENGTH(s) TO n
STOP RUN.
#include <iterator>
#include <string>
int n = std::distance(str.begin(), str.end());
#include <string>
auto n = s.size();
int n = s.Length;
size_t n = s.length;
std.range;
size_t n = s.walkLength;
import std.uni, std.range;
size_t n = s.byGrapheme.walkLength;
int n = s.length;
n = String.length s
N = string:length(S)
n = len(s)
import "unicode/utf8"
n := utf8.RuneCountInString(s)
n = length s
const n = s.length;
int n = s.length();
val n = s.length
(setf n (length s))
(length s)
local n = utf8.len(s)
$n = mb_strlen($s, 'UTF-8');
uses LazUtf8;
n := Utf8Length(s);
uses LazUtf8;
n := length(s);
uses sysutils;
n := s.length;
my $n = length( $s );
n = len(s)
n = s.length
n = s.size
let n = s.chars().count();
val n = s.length()
Dim s As String = "1234567"
Dim n As Integer = s.Length

Do you know the best way to do this in your language ?
New implementation...

Idiom created by

programming-idioms.org