mb_strlen

mb_strlen(string $string, ?string $encoding = null): int

Gets the character count of a string (multibyte-safe), contrasted with the byte count.

C C23

C23
#include <stdio.h>
#include <string.h>

static size_t utf8_len(const char *s) {
    size_t chars = 0;
    for (size_t i = 0; s[i]; i++) {
        if ((s[i] & 0xC0) != 0x80) chars++;
    }
    return chars;
}

int main(void) {
    printf("%zu\n", utf8_len("你好世界"));
    printf("%zu\n", strlen("你好世界"));
    return 0;
}
Output
4
12

C++ C++26

C++26
#include <cstring>
#include <iostream>

static size_t utf8_len(const char *s) {
    size_t chars = 0;
    for (size_t i = 0; s[i]; i++) {
        if ((s[i] & 0xC0) != 0x80) chars++;
    }
    return chars;
}

int main() {
    std::cout << utf8_len("你好世界") << "\n";
    std::cout << strlen("你好世界") << "\n";
    return 0;
}
Output
4
12

C# v11.0.100

v11.0.100
Console.WriteLine("你好世界".Length);
Console.WriteLine(System.Text.Encoding.UTF8.GetByteCount("你好世界"));
Output
4
12

Go v1.27.4

v1.27.4
package main

import (
    "fmt"
    "unicode/utf8"
)

func main() {
    fmt.Println(utf8.RuneCountInString("你好世界"))
    fmt.Println(len("你好世界"))
}
Output
4
12

Java v27.0.1

v27.0.1
import java.nio.charset.StandardCharsets;

public class Main {
    public static void main(String[] args) {
        System.out.println("你好世界".length());
        System.out.println("你好世界".getBytes(StandardCharsets.UTF_8).length);
    }
}
Output
4
12

JavaScript v26.13.1

v26.13.1
console.log("你好世界".length);
console.log(Buffer.byteLength("你好世界"));
Output
4
12

PHP v8.6.0

v8.6.0
echo mb_strlen("你好世界"), "\n";
echo strlen("你好世界");
Output
4
12

Python v3.15.2

v3.15.2
print(len("你好世界"))
print(len("你好世界".encode("utf-8")))
Output
4
12

Rust v1.99.1

v1.99.1
fn main() {
    let s = "你好世界";
    println!("{}", s.chars().count());
    println!("{}", s.len());
}
Output
4
12

TypeScript v26.13.1

v26.13.1
console.log("你好世界".length);
console.log(Buffer.byteLength("你好世界"));
Output
4
12

Language differences

C: no standard multibyte length — the idiom is a hand-written scan counting non-continuation bytes (UTF-8 continuation bytes are always 10xxxxxx, tested by masking with 0xC0).

C++: same as C — hand-written scan counting non-continuation bytes.

C#: string.Length returns 4 for BMP CJK characters; byte counts need explicit encoding via Encoding.UTF8.GetByteCount.

Go: len counts bytes while utf8.RuneCountInString counts characters.

Java: String.length (UTF-16 code units) returns 4 for BMP CJK characters; byte counts via getBytes(UTF_8).

JavaScript: str.length returns 4 for BMP CJK characters; byte counts via Buffer.byteLength.

PHP: requires mb_strlen for the character count; strlen counts bytes (each CJK character is 3 bytes in UTF-8, so 4 characters are 12 bytes).

Python: the native string type is already multibyte-safe — len returns 4 for BMP CJK characters; byte counts need explicit encoding via encode.

Rust: len counts bytes and chars().count() counts characters.

TypeScript: same as JS — str.length and Buffer.byteLength.