strpos
strpos(string $haystack, string $needle, int $offset = 0): int|false
Finds the position (index) of the first occurrence of a substring.
C C23
C23
#include <stdio.h>
#include <string.h>
int main(void) {
const char *s = "hello world";
const char *pos = strstr(s, "world");
if (pos != NULL) {
printf("%ld\n", (long)(pos - s)); // pointer difference is the offset
} else {
printf("Not found\n"); // strstr returns NULL on no match, corresponding to PHP returning false
}
// byte offsets: in UTF-8 the second CJK char starts at byte 3 of the 2-char string
const char *zh = "你好";
const char *p2 = strstr(zh, "好");
printf("%ld\n", p2 ? (long)(p2 - zh) : -1L);
return 0;
}
Output
6 3
C++ C++26
C++26
#include <iostream>
#include <string>
int main() {
std::string s = "hello world";
std::cout << s.find("world") << "\n"; // npos means not found
std::cout << std::string("你好").find("好") << "\n"; // byte offset: 3 in UTF-8
return 0;
}
Output
6 3
C# v11.0.100
v11.0.100
Console.WriteLine("hello world".IndexOf("world"));
// IndexOf counts UTF-16 code units: the CJK char sits at index 1 (chars, not bytes)
Console.WriteLine("你好".IndexOf("好"));
Output
6 1
Go v1.27.4
v1.27.4
package main
import (
"fmt"
"strings"
)
func main() {
fmt.Println(strings.Index("hello world", "world"))
// Index returns a BYTE offset: 3 for the second CJK char in the 2-char string
fmt.Println(strings.Index("你好", "好"))
}
Output
6 3
Java v27.0.1
v27.0.1
public class Main {
public static void main(String[] args) {
System.out.println("hello world".indexOf("world"));
// indexOf counts UTF-16 code units: the CJK char sits at index 1 (chars, not bytes)
System.out.println("你好".indexOf("好"));
}
}
Output
6 1
JavaScript v26.13.1
v26.13.1
console.log("hello world".indexOf("world"))
// indexOf counts UTF-16 code units: the CJK char sits at index 1 (chars, not bytes)
console.log("你好".indexOf("好"))
Output
6 1
PHP v8.6.0
v8.6.0
echo strpos("hello world", "world"), "\n";
// the classic trap: a match at position 0 is falsy — compare with !== false
var_dump(strpos("hello", "h") === 0, strpos("hello", "world") === false);
// needle search counts BYTES: the second CJK char starts at byte 3 of the 2-char string
echo strpos("你好", "好");
Output
6 bool(true) bool(true) 3
Python v3.15.2
v3.15.2
print("hello world".find("world"))
print("你好".find("好")) # str indexes count characters: 1 (not bytes)
Output
6 1
Rust v1.99.1
v1.99.1
fn main() {
let s = "hello world";
match s.find("world") {
Some(i) => println!("{}", i), // hit: byte offset
None => println!("Not found"), // no match (PHP returns false in this case)
}
println!("{:?}", s.find("xyz")); // no-match demo: None
let zh = "你好";
// find() returns a BYTE offset: 3 for the second CJK char (each CJK char is 3 UTF-8 bytes)
println!("{}", zh.find("好").unwrap());
}
Output
6 None 3
TypeScript v26.13.1
v26.13.1
const s: string = "hello world";
console.log(s.indexOf("world"));
console.log("你好".indexOf("好")); // UTF-16 code units: 1 (chars, not bytes)
Output
6 1
Language differences
C: locates the substring with strstr(), which returns NULL on miss; the example checks for it (PHP returns false in that case).
Go: strings.Index returns -1; counted in bytes.
Java: indexOf returns -1; counted in UTF-16 code units.
JavaScript: indexOf returns -1; counted in UTF-16 code units.
PHP: the not-found return is false (not -1), and a match at position 0 is falsy — compare with !== false; positions are counted in bytes — this entry demonstrates with the UTF-8 string "你好", where "好" sits at byte offset 3.
Python: find returns -1; counted in Unicode code points.
Rust: find returns Option<usize> (None on miss); the example shows both the hit and miss branches.