strrpos / strripos

strrpos(string $haystack, string $needle, int $offset = 0): int|false — strripos(string $haystack, string $needle, int $offset = 0): int|false

查找子串最后一次出现的位置(strpos 家族的反向版),strripos 为其大小写不敏感变体;未找到返回 false 而非 -1,位置 0 的匹配需用 !== false 判定。

C C23

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

int main(void) {
    const char *s = "hello world hello";
    // C 没有 strrstr:用 strstr 找出所有命中并保留最后一个
    const char *last = NULL;
    for (const char *p = s; (p = strstr(p, "hello")) != NULL; ++p) {
        last = p; // 每轮前进一字节,允许重叠匹配
    }
    printf("%ld\n", last != NULL ? (long)(last - s) : -1L); // 12
    // 单字符场景:strrchr 就是标准反向查找
    printf("%ld\n", (long)(strrchr(s, 'w') - s)); // 6
    return 0;
}
输出
12
6

C++ C++26

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

int main() {
    std::string s = "hello world hello";
    std::cout << s.rfind("hello") << "\n"; // 12:最后一次出现
    std::cout << s.rfind('w') << "\n";     // 6:单字符重载,类似 strrchr
    std::cout << (s.rfind("xyz") == std::string::npos) << "\n"; // 1:npos 表示未找到
    return 0;
}
输出
12
6
1

C# v11.0.100

v11.0.100
string s = "hello world hello";
Console.WriteLine(s.LastIndexOf("hello")); // 12
Console.WriteLine(s.LastIndexOf('w'));     // 6
Console.WriteLine(s.LastIndexOf("xyz"));   // -1:未找到
// 大小写不敏感反向查找无内置——转小写副本后再查是惯用手法
输出
12
6
-1

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"strings"
)

func main() {
	s := "hello world hello"
	fmt.Println(strings.LastIndex(s, "hello")) // 12
	fmt.Println(strings.LastIndexByte(s, 'w')) // 6:单字节版,类似 strrchr
	fmt.Println(strings.LastIndex(s, "xyz"))   // -1:未找到
	// 「字符集合任一」的反向查找:LastIndexAny
	fmt.Println(strings.LastIndexAny(s, "lo")) // 16
}
输出
12
6
-1
16

Java v27.0.1

v27.0.1
public class Main {
    public static void main(String[] args) {
        String s = "hello world hello";
        System.out.println(s.lastIndexOf("hello")); // 12
        System.out.println(s.lastIndexOf('w'));     // 6
        System.out.println(s.lastIndexOf("xyz"));   // -1:未找到
        // 大小写不敏感反向查找无内置——惯用手法是两侧先转小写
    }
}
输出
12
6
-1

JavaScript v26.13.1

v26.13.1
const s = "hello world hello"
console.log(s.lastIndexOf("hello")) // 12
console.log(s.lastIndexOf("w"))     // 6
console.log(s.lastIndexOf("xyz"))   // -1:未找到
输出
12
6
-1

PHP v8.6.0

v8.6.0
$s = "hello world hello";
var_dump(strrpos($s, "hello"));       // 12:最后一次出现的位置
var_dump(strripos($s, "WORLD"));      // 6:大小写不敏感的反向查找
var_dump(strrpos($s, "xyz") === false); // 未找到返回 false(不是 -1)
输出
int(12)
int(6)
bool(true)

Python v3.15.2

v3.15.2
s = "hello world hello"
print(s.rfind("hello"))  # 12
print(s.rfind("w"))      # 6
print(s.rfind("xyz"))    # -1:未找到
输出
12
6
-1

Rust v1.99.1

v1.99.1
fn main() {
    let s = "hello world hello";
    println!("{:?}", s.rfind("hello")); // Some(12):最后一次出现
    println!("{:?}", s.rfind('w'));     // Some(6)
    println!("{:?}", s.rfind("xyz"));   // None:未找到(此处 PHP 返回 false)
}
输出
Some(12)
Some(6)
None

TypeScript v26.13.1

v26.13.1
const s: string = "hello world hello";
console.log(s.lastIndexOf("hello")); // 12
console.log(s.lastIndexOf("xyz"));   // -1:未找到
输出
12
-1

语言差异说明

C:无 strrstr(多字符反向查找不是标准函数)——示例用 strstr 循环保留最后命中;单字符反向查找是 strrchr。

C++:rfind 即原生反向族,未找到返回 npos(示例以与 npos 比较演示)。

C#:LastIndexOf 未找到返回 -1。

Go:LastIndex 未找到返回 -1;LastIndexByte 对应 strrchr;LastIndexAny 是「字符集合任一」的反向版。

Java:lastIndexOf 未找到返回 -1。

JavaScript:lastIndexOf 未找到返回 -1。

PHP:strrpos 未找到返回 false(不是 -1);$offset 为正时限定「从该位置往右」的区间、为负时从尾部倒数——读手册示例时方向极易搞反;strripos 是大小写不敏感版,多字节场景请用 mb_strripos。

Python:rfind 未找到返回 -1。

Rust:rfind 返回 Option<usize>,未找到为 None。

TypeScript:同 JavaScript。