mb_strpos / mb_stripos / mb_strrpos / mb_strripos
mb_strpos(string $haystack, string $needle, int $offset = 0, ?string $encoding = null): int|false — mb_stripos(...):忽略大小写版 — mb_strrpos(...):从后向前找 — mb_strripos(...):从后向前 + 忽略大小写
Multibyte-safe search returning CHARACTER positions - the core contrast with strpos's byte positions: for '世界' in '你好,世界', mb gives 3 while bytes give 9.
C C23
#include <stdio.h>
#include <string.h>
int main(void) {
const char *h = "你好,世界";
const char *hit = strstr(h, "世界");
if (hit != NULL) {
printf("%ld\n", (long) (hit - h)); /* 9: BYTE offset - strstr knows no characters */
printf("%zu\n", strlen("你")); /* 3: one CJK char is 3 UTF-8 bytes */
} else {
printf("not found\n");
}
if (strstr(h, "xyz") == NULL) {
printf("missing\n"); /* strstr signals by NULL, not -1/false */
}
return 0;
}
9 3 missing
C++ C++26
#include <iostream>
#include <string>
int main() {
std::string h = "你好,世界";
std::cout << h.find("世界") << "\n"; // 9: byte offset, same as strstr
std::cout << (h.find("xyz") == std::string::npos) << "\n"; // 1: npos marks missing
std::cout << h.rfind("好") << "\n"; // 3: byte offset of the LAST match
return 0;
}
9 1 3
C# v11.0.100
var h = "你好,世界";
Console.WriteLine(h.IndexOf("世界")); // 3: .NET strings are UTF-16 - BMP chars are 1 unit
Console.WriteLine(h.IndexOf("xyz")); // -1
Console.WriteLine(h.LastIndexOf("好")); // 1: the second character
Console.WriteLine("Hello World".IndexOf("world",
System.StringComparison.OrdinalIgnoreCase)); // 6: case-insensitive via comparison mode
3 -1 1 6
Go v1.27.4
package main
import (
"fmt"
"strings"
"unicode/utf8"
)
func main() {
h := "你好,世界"
fmt.Println(strings.Index(h, "世界")) // 9: BYTE offset, exactly like PHP's strpos!
// converting a byte offset to a character offset needs a manual count:
bytePos := strings.Index(h, "世界")
fmt.Println(utf8.RuneCountInString(h[:bytePos])) // 3
fmt.Println(strings.LastIndex(h, "好")) // 3 bytes
fmt.Println(strings.Index(h, "xyz")) // -1
fmt.Println(strings.Index(strings.ToLower("Hello World"), "world")) // 6: manual case fold
}
9 3 3 -1 6
Java v27.0.1
public class Main {
public static void main(String[] args) {
String h = "你好,世界";
System.out.println(h.indexOf("世界")); // 3: UTF-16 code units - BMP chars are 1
System.out.println(h.lastIndexOf("好")); // 1: the second character
System.out.println(h.indexOf("xyz")); // -1
System.out.println("Hello World".toLowerCase().indexOf("world")); // 6: manual fold
}
}
3 1 -1 6
JavaScript v26.13.1
const h = "你好,世界";
console.log(h.indexOf("世界")); // 3: natively character-level (UTF-16 units) - no mb_ prefix needed
console.log(h.lastIndexOf("好")); // 1: the second character
console.log(h.indexOf("xyz")); // -1
console.log("Hello World".toLowerCase().indexOf("world")); // 6: manual fold
3 1 -1 6
PHP v8.6.0
// character position vs byte position: the whole point of the mb_ family
$h = '你好,世界';
var_dump(mb_strpos($h, '世界')); // 3: CHARACTER position
var_dump(strpos($h, '世界')); // 9: BYTE position (each CJK char is 3 bytes)
var_dump(mb_strrpos('今天很好,今天真开心', '今天')); // 5: last match, in characters
var_dump(mb_stripos('Hello World', 'world')); // 6: case-insensitive, forward
var_dump(mb_strripos('Hello World', 'world')); // 6: case-insensitive, backward
var_dump(mb_strpos($h, '不存在')); // false: not found (careful with ===)
int(3) int(9) int(5) int(6) int(6) bool(false)
Python v3.15.2
h = "你好,世界"
print(h.find("世界")) # 3: natively character-indexed, no mb_ prefix needed
print(h.rfind("好")) # 1: the second character
print(h.find("xyz")) # -1 for missing (vs PHP's false)
try:
h.index("xyz") # index() raises instead of returning -1
except ValueError:
print("ValueError")
print("Hello World".lower().find("world")) # 6: manual case fold
3 1 -1 ValueError 6
Rust v1.99.1
fn main() {
let h = "你好,世界";
println!("{}", h.find("世界").unwrap()); // 9: find returns BYTE offsets, like strpos!
let byte = h.find("世界").unwrap();
println!("{}", h[..byte].chars().count()); // 3: convert to char position manually
println!("{}", h.rfind("好").unwrap()); // 3 bytes
println!("{}", h.find("xyz").is_none()); // true: Option marks missing
println!("{}", "Hello World".to_lowercase().find("world").unwrap()); // 6
}
9 3 3 true 6
TypeScript v26.13.1
const h = "你好,世界";
console.log(h.indexOf("世界")); // 3: natively character-level (UTF-16 units)
console.log(h.lastIndexOf("好")); // 1: the second character
console.log(h.indexOf("xyz")); // -1
console.log("Hello World".toLowerCase().indexOf("world")); // 6
3 1 -1 6
Language differences
C: strstr returns a pointer and the byte offset comes from pointer subtraction (9 in the example); the case-insensitive variant is strcasestr (a POSIX extension, not standard C); not-found is signaled by NULL.
C++: std::string::find/rfind return byte offsets (the example hits 9 for "世界"), with npos marking not-found; there is no case-insensitive version - fold the whole string with tolower by hand.
C#: UTF-16 code-unit indexing - BMP characters are naturally 1 unit = 1 position (3 in the example), while supplementary-plane characters occupy 2 units (surrogate pairs) and skew positions; case-insensitivity uses the StringComparison overload of IndexOf (no separate function).
Go: strings.Index/LastIndex return BYTE offsets - the same trap as PHP's strpos (9 in the example); the character position needs a manual utf8.RuneCountInString(h[:pos]); no case-insensitive variant, the idiom is strings.ToLower first.
Java: UTF-16 code-unit indexing with BMP characters naturally aligned (3); the supplementary plane has the same surrogate-pair issue as JS; no builtin case-insensitive search - toLowerCase() is the idiom.
JavaScript: indexOf/lastIndexOf are natively character-level (UTF-16 units) - NO dual track needed: BMP scenes align directly (3), and supplementary-plane characters (2 units each) are the only exception; case-insensitivity means a manual toLowerCase.
PHP: mb_strpos returns CHARACTER positions while strpos returns BYTE positions - the same search yields 3 and 9 (example), which is the entire reason the mb family exists; the four functions form a 2x2 matrix of direction x case; not-found returns false (not -1), so test with === false to avoid the 0 trap; the offset parameter counts characters too.
Python: natively character-indexed - find/rfind need no mb prefix (3); not-found is -1 (PHP uses false); the index variant raises ValueError (the exception camp). No builtin case-insensitive search; lower() as a relay.
Rust: str::find/rfind return BYTE offsets (same as Go's strpos flavor, 9 in the example) - because a byte offset is a legal O(1) slice boundary; the char position needs h[..byte].chars().count() by hand; not-found is Option::None (typed, not a magic value).
TypeScript: same as JavaScript; at the type level indexOf returns number, indistinguishable between "position 0" and "not found -1" - the runtime check remains essential.