stripos / stristr

stripos(string $haystack, string $needle, int $offset = 0): int|false — stristr(string $haystack, string $needle, bool $before_needle = false): string|false

大小写不敏感查找:stripos 返回首现位置(int|false,用 === 判假),stristr 返回自命中到末尾的串(before_needle 取头部,8.3+);仅 ASCII 折叠。

C C23

C23
/* strcasestr(3) 不可移植:它是 GNU 扩展(macOS 没有)。
   可移植惯用法:ASCII 小写副本 + strstr(3);
   同时演示 stristr 的尾部/头部切分。 */
#include <ctype.h>
#include <stdio.h>
#include <string.h>

static void ascii_lower(char *dst, const char *src) {
    size_t i = 0;
    for (; src[i]; ++i) dst[i] = (char)tolower((unsigned char)src[i]);
    dst[i] = '\0';
}

int main(void) {
    char h[32], n[8];
    ascii_lower(h, "Hello World");
    ascii_lower(n, "world");
    char *hit = strstr(h, n);
    printf("%d\n", hit != NULL);
    printf("%d\n", (int)(hit - h));
    ascii_lower(h, "user@example.com");
    ascii_lower(n, "@");
    char *at = strstr(h, n);
    printf("%s\n", at);        /* 尾部 = stristr */
    *at = '\0';
    printf("%s\n", h);         /* 头部 = before_needle */
    return 0;
}
输出
1
6
@example.com
user

C++ C++26

C++26
// std::search + 大小写折叠谓词:C++ 表达 stripos 的方式。
#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>

static bool fold_eq(char a, char b) { // 大小写不敏感的单字符相等
    return std::tolower((unsigned char)a) == std::tolower((unsigned char)b);
}

int main() {
    std::string h = "Hello World";
    auto it = std::search(h.begin(), h.end(), std::begin("world"), std::end("world") - 1, fold_eq);
    std::cout << (it != h.end()) << "\n";
    std::cout << (it - h.begin()) << "\n";
    std::string email = "user@example.com";
    auto at = std::search(email.begin(), email.end(), std::begin("@"), std::end("@") - 1, fold_eq);
    std::cout << std::string(at, email.end()) << "\n";  // 尾部 = stristr
    std::cout << std::string(email.begin(), at) << "\n"; // 头部 = before_needle
}
输出
1
6
@example.com
user

C# v11.0.100

v11.0.100
// IndexOf + OrdinalIgnoreCase 即 stripos;
// 从命中处 Substring 复刻 stristr(尾部 / before_needle 头部)。
var h = "Hello World";
int pos = h.IndexOf("world", StringComparison.OrdinalIgnoreCase);
Console.WriteLine(pos >= 0);
Console.WriteLine(pos);
var email = "user@example.com";
int at = email.IndexOf("@", StringComparison.OrdinalIgnoreCase);
Console.WriteLine(email[at..]);       // 尾部
Console.WriteLine(email[..at]);       // 头部(before_needle)
输出
True
6
@example.com
user

Go v1.27.4

v1.27.4
// strings.Index 按字节位置;双方用 strings.ToLower 折叠
// (ASCII 安全:ASCII needle 的小写折叠不改变字节偏移)。
package main

import (
	"fmt"
	"strings"
)

func main() {
	h := strings.ToLower("Hello World")
	pos := strings.Index(h, strings.ToLower("world"))
	fmt.Println(pos >= 0)
	fmt.Println(pos)
	email := strings.ToLower("user@example.com")
	at := strings.Index(email, "@")
	fmt.Println(email[at:])  // 尾部 = stristr
	fmt.Println(email[:at])  // 头部 = before_needle
}
输出
true
6
@example.com
user

Java v27.0.1

v27.0.1
// 小写副本上 indexOf:stripos 惯用法;substring 切出
// stristr 的尾部 / before_needle 头部。
public class Main {
    public static void main(String[] args) {
        String h = "Hello World".toLowerCase();
        int pos = h.indexOf("world");
        System.out.println(pos >= 0);
        System.out.println(pos);
        String email = "user@example.com";
        int at = email.indexOf('@');
        System.out.println(email.substring(at)); // 尾部
        System.out.println(email.substring(0, at)); // 头部
    }
}
输出
true
6
@example.com
user

JavaScript v26.13.1

v26.13.1
// 小写化 + indexOf / slice:stripos / stristr 惯用法。
const h = "Hello World".toLowerCase();
const pos = h.indexOf("world");
console.log(pos >= 0);
console.log(pos);
const email = "user@example.com";
const at = email.indexOf("@");
console.log(email.slice(at));  // 尾部 = stristr
console.log(email.slice(0, at)); // 头部 = before_needle
输出
true
6
@example.com
user

PHP v8.6.0

v8.6.0
// stripos / stristr - 大小写不敏感查找(8.2 起仅 ASCII 折叠)。
// stripos 返回位置(int|false);stristr 返回命中到末尾的串,
// before_needle 参数翻转取头部。
var_dump(stripos("Hello World", "world")); // 6
var_dump(stripos("Hello World", "XYZ") === false);
var_dump(stripos("abcabc", "B", 2)); // offset 2 -> 下一个 'B' 在 4
var_dump(stristr("user@example.com", "@")); // "@example.com"
var_dump(stristr("user@example.com", "@", true)); // "user"(before_needle)
输出
int(6)
bool(true)
int(4)
string(12) "@example.com"
string(4) "user"

Python v3.15.2

v3.15.2
# 小写副本上 find:stripos 惯用法;切片给出 stristr 尾部/头部。
h = "Hello World".lower()
pos = h.find("world")
print(pos >= 0)
print(pos)
email = "user@example.com"
at = email.find("@")
print(email[at:])   # 尾部
print(email[:at])   # 头部(before_needle)
输出
True
6
@example.com
user

Rust v1.99.1

v1.99.1
// to_ascii_lowercase 副本 + find:ASCII needle 下字节偏移依然有效。
fn main() {
    let h = "Hello World".to_ascii_lowercase();
    let pos = h.find("world");
    println!("{}", pos.is_some());
    println!("{}", pos.unwrap());
    let email = "user@example.com".to_ascii_lowercase();
    let at = email.find('@').unwrap();
    println!("{}", &email[at..]);  // 尾部 = stristr
    println!("{}", &email[..at]);  // 头部 = before_needle
}
输出
true
6
@example.com
user

TypeScript v26.13.1

v26.13.1
// 同 JavaScript:小写化后 indexOf / slice。
const h: string = "Hello World".toLowerCase();
const pos: number = h.indexOf("world");
console.log(pos >= 0);
console.log(pos);
const email: string = "user@example.com";
const at: number = email.indexOf("@");
console.log(email.slice(at));
console.log(email.slice(0, at));
输出
true
6
@example.com
user

语言差异说明

C:任务清单里的 strcasestr(3) 是 GNU 扩展——glibc 有、macOS 没有,跨平台代码走「ASCII 小写副本 + strstr」惯用法(本条 C 块即此方案);Windows 对应 _stricmp 风格搜索同样靠小写副本。C++:std::search + 折叠谓词是标准库表达。C#:IndexOf(value, StringComparison.OrdinalIgnoreCase) 返回下标;切分即 stristr。Go:strings.Index(ToLower(h), ToLower(n))——ASCII 折叠不改字节长度,偏移安全;非 ASCII 时 rune 计数与字节偏移可能错位,需 strings.ToLower 前后一致处理。Java:小写副本 indexOf 即惯用法(regionMatches(true,…) 只回 bool 不回位置)。JavaScript/TypeScript:toLowerCase + indexOf/slice;正则 /world/i 是另一常见写法(search() 回下标)。PHP:与 strpos/strstr(见 strstr 条目若存在,或 strpos 家族条目)镜像;8.2 起仅 ASCII 折叠、不依赖 locale;返回 0 表示首字符命中——必须 !== false 判未命中(0 是假值陷阱);offset 负值从尾部起算(7.1+),offset 越界抛 ValueError。Python:lower() + find()(或 re.IGNORECASE)。Rust:to_ascii_lowercase + find 与 PHP ASCII 语义对齐。教学锚点:大小写不敏感查找没有跨语言统一 API——有的语言给位置(stripos 语义),有的给子串(stristr 语义),有的两者都要手写组合。