str_contains

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

Determines if a string contains a given substring and returns a boolean.

C C23

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

// Hand-written idiom: strstr returns a non-NULL pointer when the substring is found
static int str_contains(const char *h, const char *n) {
    return strstr(h, n) != NULL;
}

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

C++ C++26

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

// Hand-written idiom: find returns npos when not found
static bool str_contains(const std::string &h, const std::string &n) {
    return h.find(n) != std::string::npos;
}

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

C# v11.0.100

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

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"strings"
)

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

Java v27.0.1

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

JavaScript v26.13.1

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

PHP v8.6.0

v8.6.0
// Available since PHP 8.0: tests substring presence, returning a bool directly
var_dump(str_contains("Hello world", "world"));
var_dump(str_contains("Hello world", "php"));
// The empty string counts as "present": every string contains it
var_dump(str_contains("Hello", ""));
Output
bool(true)
bool(false)
bool(true)

Python v3.15.2

v3.15.2
# Substring containment: the in operator (the empty string is always True)
print("world" in "Hello world")
print("php" in "Hello world")
print("" in "Hello")
Output
True
False
True

Rust v1.99.1

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

TypeScript v26.13.1

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

Language differences

C: no built-in — the idiom is strstr($haystack, $needle) != NULL; an empty needle returns the whole string pointer, so it is always true.

C++: the idiom is find() != npos; string::contains only arrives with C++23.

C#: string.Contains(string).

Go: strings.Contains.

Java: String.contains(CharSequence).

JavaScript: String.prototype.includes (introduced in ES2015; before that the idiom was indexOf(...) !== -1).

PHP: introduced in PHP 8.0, replacing the PHP 7 idiom strpos($haystack, $needle) !== false (easy to forget !== false and treat the offset as a boolean); an empty needle returns true, consistent with strpos returning offset 0.

Python: the in operator on strings is substring containment (empty string is always True).

Rust: str::contains.

TypeScript: same as JavaScript's includes.