str_starts_with

str_starts_with(string $haystack, string $needle): bool

Checks if a string starts with a given prefix.

C C23

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

// Hand-written idiom: strncmp compares only the prefix length, returning 0 on equality
static int str_starts_with(const char *h, const char *n) {
    return strncmp(h, n, strlen(n)) == 0;
}

int main(void) {
    printf("%d %d %d\n", str_starts_with("Hello world", "Hello"),
           str_starts_with("Hello world", "world"), str_starts_with("Hello", ""));
    return 0;
}
Output
1 0 1

C++ C++26

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

// Hand-written idiom: rfind from position 0 — a hit means it is a prefix
static bool str_starts_with(const std::string &h, const std::string &n) {
    return h.rfind(n, 0) == 0;
}

int main() {
    std::cout << str_starts_with("Hello world", "Hello") << " "
              << str_starts_with("Hello world", "world") << " "
              << str_starts_with("Hello", "") << "\n";
    return 0;
}
Output
1 0 1

C# v11.0.100

v11.0.100
// StartsWith tests the prefix; the empty string is always true
Console.WriteLine("Hello world".StartsWith("Hello"));
Console.WriteLine("Hello world".StartsWith("world"));
Console.WriteLine("Hello".StartsWith(""));
Output
True
False
True

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"strings"
)

func main() {
	// strings.HasPrefix tests the prefix; the empty string is always true
	fmt.Println(strings.HasPrefix("Hello world", "Hello"))
	fmt.Println(strings.HasPrefix("Hello world", "world"))
	fmt.Println(strings.HasPrefix("Hello", ""))
}
Output
true
false
true

Java v27.0.1

v27.0.1
public class Main {
    public static void main(String[] args) {
        // startsWith tests the prefix; the empty string is always true
        System.out.println("Hello world".startsWith("Hello"));
        System.out.println("Hello world".startsWith("world"));
        System.out.println("Hello".startsWith(""));
    }
}
Output
true
false
true

JavaScript v26.13.1

v26.13.1
// startsWith tests the prefix (introduced in ES2015; the empty string is always true)
console.log("Hello world".startsWith("Hello"));
console.log("Hello world".startsWith("world"));
console.log("Hello".startsWith(""));
Output
true
false
true

PHP v8.6.0

v8.6.0
// Available since PHP 8.0: tests the prefix, returning a bool directly
var_dump(str_starts_with("Hello world", "Hello"));
var_dump(str_starts_with("Hello world", "world"));
// The empty prefix is always true
var_dump(str_starts_with("Hello", ""));
Output
bool(true)
bool(false)
bool(true)

Python v3.15.2

v3.15.2
# startswith tests the prefix (the empty string is always True)
print("Hello world".startswith("Hello"))
print("Hello world".startswith("world"))
print("Hello".startswith(""))
Output
True
False
True

Rust v1.99.1

v1.99.1
fn main() {
    // starts_with tests the prefix; the empty string is always true
    println!("{}", "Hello world".starts_with("Hello"));
    println!("{}", "Hello world".starts_with("world"));
    println!("{}", "Hello".starts_with(""));
}
Output
true
false
true

TypeScript v26.13.1

v26.13.1
// startsWith tests the prefix (introduced in ES2015; the empty string is always true)
console.log("Hello world".startsWith("Hello"));
console.log("Hello world".startsWith("world"));
console.log("Hello".startsWith(""));
Output
true
false
true

Language differences

C: the idiom is strncmp($haystack, $needle, strlen($needle)) == 0; with an empty needle strlen is 0, so strncmp returns 0 and it is always true.

C++: the idiom is rfind($needle, 0) == 0; starts_with only becomes standard in C++20.

C#: string.StartsWith.

Go: strings.HasPrefix.

Java: String.startsWith.

JavaScript: String.prototype.startsWith (ES2015; before that the idioms were lastIndexOf($needle, 0) === 0 or a regex).

PHP: introduced in PHP 8.0, replacing the old substr($haystack, 0, strlen($needle)) === $needle dance (measure the length, slice, strict-compare — verbose and error-prone).

Python: str.startswith (also accepts a tuple to test several prefixes at once).

Rust: str::starts_with.

TypeScript: same as JavaScript's startsWith.