str_contains

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

判断字符串是否包含指定子串,返回布尔值。

C C23

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

// 手写惯用法:strstr 找到子串返回非空指针
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;
}
输出
1 0 1

C++ C++26

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

// 手写惯用法:find 找不到返回 npos(最大值哨兵)
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;
}
输出
1 0 1

C# v11.0.100

v11.0.100
// Contains 判断子串是否包含;空串恒为 true
Console.WriteLine("Hello world".Contains("world"));
Console.WriteLine("Hello world".Contains("php"));
Console.WriteLine("Hello".Contains(""));
输出
True
False
True

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"strings"
)

func main() {
	// strings.Contains 判断子串是否包含;空串恒为 true
	fmt.Println(strings.Contains("Hello world", "world"))
	fmt.Println(strings.Contains("Hello world", "php"))
	fmt.Println(strings.Contains("Hello", ""))
}
输出
true
false
true

Java v27.0.1

v27.0.1
public class Main {
    public static void main(String[] args) {
        // contains 判断子串是否包含;空串恒为 true
        System.out.println("Hello world".contains("world"));
        System.out.println("Hello world".contains("php"));
        System.out.println("Hello".contains(""));
    }
}
输出
true
false
true

JavaScript v26.13.1

v26.13.1
// includes 判断子串(ES2015 引入;空串恒为 true)
console.log("Hello world".includes("world"));
console.log("Hello world".includes("php"));
console.log("Hello".includes(""));
输出
true
false
true

PHP v8.6.0

v8.6.0
// PHP 8.0 起可用:判断子串是否存在,直接返回布尔值
var_dump(str_contains("Hello world", "world"));
var_dump(str_contains("Hello world", "php"));
// 空串按「存在」处理:任何串都包含空串
var_dump(str_contains("Hello", ""));
输出
bool(true)
bool(false)
bool(true)

Python v3.15.2

v3.15.2
# 子串包含判断:in 运算符(空串恒为 True)
print("world" in "Hello world")
print("php" in "Hello world")
print("" in "Hello")
输出
True
False
True

Rust v1.99.1

v1.99.1
fn main() {
    // contains 判断子串是否包含;空串恒为 true
    println!("{}", "Hello world".contains("world"));
    println!("{}", "Hello world".contains("php"));
    println!("{}", "Hello".contains(""));
}
输出
true
false
true

TypeScript v26.13.1

v26.13.1
// includes 判断子串(ES2015 引入;空串恒为 true)
console.log("Hello world".includes("world"));
console.log("Hello world".includes("php"));
console.log("Hello".includes(""));
输出
true
false
true

语言差异说明

C:无内置,惯用法 strstr($haystack, $needle) != NULL;空 needle 返回整串指针故恒真。

C++:惯用法 find() != npos;C++23 起才有 string::contains。

C#:string.Contains(string)。

Go:strings.Contains。

Java:String.contains(CharSequence)。

JavaScript:String.prototype.includes(ES2015 引入;此前惯用 indexOf(...) !== -1)。

PHP:PHP 8.0 引入,取代 PHP 7 时代的惯用老写法 strpos($haystack, $needle) !== false(易忘写 !== false 而错把偏移量当布尔);空 needle 返回 true,与 strpos 偏移 0 的语义一致。

Python:in 运算符对字符串即子串包含(空串恒为 True)。

Rust:str::contains。

TypeScript:同 JavaScript 的 includes。