stripos / stristr
stripos(string $haystack, string $needle, int $offset = 0): int|false — stristr(string $haystack, string $needle, bool $before_needle = false): string|false
Case-insensitive search: stripos returns the first-match position (int|false, test with ===), stristr returns the tail from the match (before_needle flips to the head); ASCII-only folding.
C C23
/* No portable strcasestr(3): it is a GNU extension (macOS lacks it).
The portable idiom lowers ASCII copies, then uses strstr(3);
this also demonstrates the stristr tail/head split. */
#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); /* tail = stristr */
*at = '\0';
printf("%s\n", h); /* head = before_needle */
return 0;
}
1 6 @example.com user
C++ C++26
// std::search with a case-folding predicate: the C++ way to express stripos.
#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>
static bool fold_eq(char a, char b) { // ASCII-insensitive char equality
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"; // tail = stristr
std::cout << std::string(email.begin(), at) << "\n"; // head = before_needle
}
1 6 @example.com user
C# v11.0.100
// IndexOf with OrdinalIgnoreCase is the stripos equivalent;
// Substring from the hit reproduces stristr (tail / before_needle head).
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..]); // tail
Console.WriteLine(email[..at]); // head (before_needle)
True 6 @example.com user
Go v1.27.4
// strings.Index is byte-position based; fold both sides with strings.ToLower
// (ASCII-safe: folding preserves byte offsets for ASCII needles).
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:]) // tail = stristr
fmt.Println(email[:at]) // head = before_needle
}
true 6 @example.com user
Java v27.0.1
// indexOf on lowercased copies: the stripos idiom; substring splits give
// the stristr tail / before_needle head.
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)); // tail
System.out.println(email.substring(0, at)); // head
}
}
true 6 @example.com user
JavaScript v26.13.1
// Lowercase both sides, then indexOf / slice: the stripos / stristr idiom.
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)); // tail = stristr
console.log(email.slice(0, at)); // head = before_needle
true 6 @example.com user
PHP v8.6.0
// stripos / stristr - case-INSENSITIVE search (ASCII folding since 8.2).
// stripos returns the position (int|false); stristr returns the tail from the
// match (string|false), with before_needle for the head instead.
var_dump(stripos("Hello World", "world")); // 6
var_dump(stripos("Hello World", "XYZ") === false);
var_dump(stripos("abcabc", "B", 2)); // offset 2 -> next 'B' at 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
# find on lowercased copies: the stripos idiom; slicing gives the stristr tail/head.
h = "Hello World".lower()
pos = h.find("world")
print(pos >= 0)
print(pos)
email = "user@example.com"
at = email.find("@")
print(email[at:]) # tail
print(email[:at]) # head (before_needle)
True 6 @example.com user
Rust v1.99.1
// to_ascii_lowercase copies + find: byte offsets stay valid for ASCII needles.
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..]); // tail = stristr
println!("{}", &email[..at]); // head = before_needle
}
true 6 @example.com user
TypeScript v26.13.1
// Same as JavaScript: lowercase, then 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
Language differences
C: strcasestr(3) from the task list is a GNU extension - glibc has it, macOS does not; portable code uses the "ASCII-lowercase copies + strstr" idiom (exactly this C block); Windows _stricmp-style search likewise relies on lowercase copies. C++: std::search with a fold predicate is the standard-library spelling. C#: IndexOf(value, StringComparison.OrdinalIgnoreCase) returns the index; slicing reproduces stristr. Go: strings.Index(ToLower(h), ToLower(n)) - ASCII folding preserves byte length, so offsets stay valid; for non-ASCII, rune counts may drift from byte offsets unless ToLower is applied consistently. Java: indexOf on lowercased copies is the idiom (regionMatches(true, ...) returns bool only). JavaScript/TypeScript: toLowerCase + indexOf/slice; the /world/i regex is another common route (search() returns the index). PHP: mirrors strpos/strstr; since 8.2 ASCII-only folding, locale-free; a return of 0 means a hit at position zero - test !== false for miss (the 0-is-falsy trap); negative offset counts from the end (7.1+), out-of-range offset throws ValueError. Python: lower() + find() (or re.IGNORECASE). Rust: to_ascii_lowercase + find aligns with PHP ASCII semantics. Teaching anchor: there is no unified cross-language API for case-insensitive search - some languages return a position (stripos semantics), some return a substring (stristr semantics), some require hand-written composition for both.