substr
substr(string $string, int $offset, ?int $length = null): string
Extracts a portion of a string.
C C23
#include <stdio.h>
int main(void) {
const char *s = "hello world";
printf("%.*s\n", 5, s + 6); // precision caps the length; pointer arithmetic sets the start
// pointer arithmetic works in bytes: offset 3 skips the 3 bytes of the first CJK char
const char *zh4 = "你好世界";
printf("%.3s\n", zh4 + 3); // one character: 3 UTF-8 bytes
return 0;
}
world 好
C++ C++26
#include <iostream>
#include <string>
int main() {
std::string s = "hello world";
std::cout << s.substr(6, 5) << "\n"; // offset 6, length 5
// substr works in bytes: offset 3, length 3 = one CJK character
std::cout << std::string("你好世界").substr(3, 3) << "\n";
return 0;
}
world 好
C# v11.0.100
Console.WriteLine("hello world".Substring(6, 5));
// Substring works in UTF-16 code units: index 1 = one character
Console.WriteLine("你好世界".Substring(1, 1));
world 好
Go v1.27.4
package main
import "fmt"
func main() {
fmt.Println("hello world"[6:11])
// slicing works in bytes: [3:6] is the second character
fmt.Println("你好世界"[3:6])
}
world 好
Java v27.0.1
public class Main {
public static void main(String[] args) {
System.out.println("hello world".substring(6, 11));
// substring works in UTF-16 code units: index 1 = one character
System.out.println("你好世界".substring(1, 2));
}
}
world 好
JavaScript v26.13.1
console.log("hello world".substring(6, 11))
// slice works in UTF-16 code units: index 1 = one character
console.log("你好世界".slice(1, 2))
world 好
PHP v8.6.0
echo substr("hello world", 6, 5), "\n";
// substr works in BYTES: offset 3 skips the 3 bytes of the first CJK char
echo substr("你好世界", 3, 3), " vs ", mb_substr("你好世界", 1, 1);
world 好 vs 好
Python v3.15.2
print("hello world"[6:11])
print("你好世界"[1:2]) # str slicing counts characters, not bytes
world 好
Rust v1.99.1
fn main() {
let s = "hello world";
// byte-index slicing: start 6, end 11; out-of-range panics, non-ASCII must slice at char boundaries
println!("{}", &s[6..11]);
// byte slicing must land on char boundaries: [3..6] is the second character
println!("{}", &"你好世界"[3..6]);
}
world 好
TypeScript v26.13.1
console.log("hello world".slice(6, 11));
console.log("你好世界".slice(1, 2)); // UTF-16 code units: index 1 = one character
world 好
Language differences
C: limits the length with the %.*s precision and offsets a pointer to the start; the example covers a positive offset and length — negative offsets are not implemented.
Go: slice syntax [start:end]; indexes counted in bytes.
Java: substring(begin, end); indexes counted in UTF-16 code units.
JavaScript: substring(start, end); indexes counted in UTF-16 code units.
PHP: takes the length as its third argument, while most other languages take an end index; index/length units differ — PHP/Go count bytes, Python counts Unicode code points, JS/Java count UTF-16 code units; this entry demonstrates the units with the UTF-8 string "你好世界", where the same character sits at different indices under byte vs character counting.
Python: slice syntax [start:end]; indexes counted in Unicode code points.
Rust: slices by byte index (&s[6..11]); out-of-bounds panics, and non-ASCII text must be sliced at char boundaries.