str_split

str_split(string $string, int $length = 1): array

Splits a string into an array: single bytes by default, or fixed-length chunks; on multi-byte strings it splits BYTES, not characters (mb_str_split is the character-level counterpart).

C C23

C23
#include <stdio.h>

int main(void) {
    // a C string IS a byte array: iterate it for the byte-level str_split
    const char *s = "Hello";
    for (const char *p = s; *p; p++) {
        if (p != s) { printf(" "); } // separator before every element but the first
        printf("%c", *p);
    }
    printf("\n");
    // UTF-8 strings split as bytes, never as characters
    printf("%zu\n", sizeof("你好") - 1); // 6 bytes (+1 terminator)
    return 0;
}
Output
H e l l o
6

C++ C++26

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

int main() {
    std::string s = "Hello";
    for (std::size_t i = 0; i < s.size(); i++) {
        if (i > 0) { std::cout << ' '; } // separator before every element but the first
        std::cout << s[i];
    }
    std::cout << "\n";
    std::cout << std::string("你好").size() << "\n"; // 6 bytes, not 2 characters
    return 0;
}
Output
H e l l o
6

C# v11.0.100

v11.0.100
// ToCharArray splits UTF-16 code units (chars)
Console.WriteLine(string.Join(",", "Hello".ToCharArray())); // H,e,l,l,o
Console.WriteLine("Hello".Length);            // 5 UTF-16 units
Console.WriteLine("😀".Length);                // 2 units: one surrogate pair
Console.WriteLine("😀".EnumerateRunes().Count()); // 1 code point (System.Text.Rune)
Output
H,e,l,l,o
5
2
1

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Println(strings.Split("Hello", "")) // [H e l l o]: split after each UTF-8 sequence
	fmt.Println(len("你好"))                  // 6 bytes
	fmt.Println(len([]rune("你好")))          // 2 runes (code points)
}
Output
[H e l l o]
6
2

Java v27.0.1

v27.0.1
public class Main {
    public static void main(String[] args) {
        System.out.println(java.util.Arrays.toString("Hello".toCharArray())); // UTF-16 units
        System.out.println("Hello".length());          // 5 UTF-16 units
        System.out.println("😀".length());              // 2 units: a surrogate pair
        System.out.println("😀".codePointCount(0, "😀".length())); // 1 code point
    }
}
Output
[H, e, l, l, o]
5
2
1

JavaScript v26.13.1

v26.13.1
console.log([..."Hello"].join(",")); // H,e,l,l,o — spread splits by code point
console.log("Hello".length);          // 5 UTF-16 units
console.log([..."😀"].length);         // 1 code point
console.log("😀".length);              // 2 UTF-16 units: a surrogate pair
// JS has no chunk-length parameter: hand-roll it when needed
Output
H,e,l,l,o
5
1
2

PHP v8.6.0

v8.6.0
echo implode(',', str_split('Hello')), "\n";  // one BYTE per element by default
echo implode(',', str_split('Hello', 2)), "\n"; // the length parameter: fixed-size chunks
echo count(str_split('你好')), "\n";  // 6: bytes, not characters
echo bin2hex(str_split('你好')[0]), "\n"; // e4: a lone byte, invalid UTF-8 on its own
echo count(mb_str_split('你好')), "\n"; // 2: characters via the mbstring counterpart
Output
H,e,l,l,o
He,ll,o
6
e4
2

Python v3.15.2

v3.15.2
print(list("Hello"))            # characters (code points)
print(len("你好"))               # 2 characters
print(list("你好".encode()))      # [228, 189, 160, 229, 165, 189]: UTF-8 bytes

def chunks(s: str, n: int) -> list[str]:  # str_split's length parameter, hand-rolled
    return [s[i:i + n] for i in range(0, len(s), n)]

print(chunks("Hello", 2))         # ['He', 'll', 'o']
Output
['H', 'e', 'l', 'l', 'o']
2
[228, 189, 160, 229, 165, 189]
['He', 'll', 'o']

Rust v1.99.1

v1.99.1
fn main() {
    let chars: Vec<char> = "Hello".chars().collect(); // code points, not bytes
    println!("{}", chars.iter().collect::<String>());
    println!("{}", "你好".chars().count()); // 2 characters
    println!("{}", "你好".len());           // 6 bytes
}
Output
Hello
2
6

TypeScript v26.13.1

v26.13.1
console.log([..."Hello"].join(",")); // H,e,l,l,o — spread splits by code point
console.log("Hello".length);          // 5 UTF-16 units
console.log([..."😀"].length);         // 1 code point
console.log("😀".length);              // 2 UTF-16 units: a surrogate pair
// TS has no chunk-length parameter either: hand-roll it when needed
Output
H,e,l,l,o
5
1
2

Language differences

PHP: str_split splits BYTES (single-byte chunks by default); the UTF-8 string "你好" becomes 6 chunks and a single chunk is invalid UTF-8 on its own (see e4 in the output); mb_str_split splits by character (2 chunks).

PHP 8.2: an empty string returns [] (previously an array holding one empty string); length < 1 throws ValueError.

Python/JS/TS/Rust chars()/Go []rune: split by Unicode code point; str_split's chunk-length semantics must be hand-written (see the Python slicing demo).

JS/TS: [...s]/Array.from split by code point, while indexing and .length count UTF-16 units — "😀".length === 2 (a surrogate pair); the example contrasts both.

Java: toCharArray() gives UTF-16 units; codePointCount counts code points. C#: ToCharArray() likewise; EnumerateRunes() (System.Text.Rune) counts code points.

Go: strings.Split(s, "") splits into UTF-8 sequences (code points); len(s) is bytes, and only []rune(s) gives the character count.

Rust: "你好".chars().count() == 2 versus len() == 6 is exactly "code points vs bytes"; collecting into Vec<char> yields the character array.

C: a string IS a byte array — iterating bytes is the byte-level str_split; sizeof("你好") - 1 is 6 (plus the 1-byte terminator).

C++: range-for iterates bytes; std::string("你好").size() is 6 bytes — same as C.