strpbrk
strpbrk(string $string, string $characters): string|false
Scan for the first occurrence of ANY character from a charset: returns the tail from that position (not an index); false when nothing matches; an empty charset throws ValueError (8.0+).
C C23
/* strpbrk(3): the C original - pointer to the first hit of any charset
character; NULL when nothing matches. Offset = pointer difference. */
#include <stdio.h>
#include <string.h>
int main(void) {
const char *hit = strpbrk("hello world", "lw");
printf("%s\n", hit);
printf("%d\n", (int)(hit - "hello world"));
printf("%s\n", strpbrk("2026-11-25", "0123456789"));
printf("%d\n", strpbrk("abc", "xyz") == NULL);
return 0;
}
llo world 2 2026-11-25 1
C++ C++26
// std::string::find_first_of is the direct C++ spelling of strpbrk.
#include <iostream>
#include <string>
int main() {
std::string s = "hello world";
auto p = s.find_first_of("lw");
std::cout << s.substr(p) << "\n"; // tail, like strpbrk
std::cout << p << "\n";
std::cout << std::string("2026-11-25").substr(0) << "\n";
std::cout << (std::string("abc").find_first_of("xyz") == std::string::npos) << "\n";
}
llo world 2 2026-11-25 1
C# v11.0.100
// IndexOfAny(char[]) is the strpbrk equivalent; Substring gives the tail.
var s = "hello world";
int p = s.IndexOfAny(new[] { 'l', 'w' });
Console.WriteLine(s[p..]);
Console.WriteLine(p);
Console.WriteLine("2026-11-25".IndexOfAny("0123456789".ToCharArray()));
Console.WriteLine("abc".IndexOfAny(new[] { 'x', 'y', 'z' }) < 0);
llo world 2 0 True
Go v1.27.4
// strings.IndexAny returns the byte offset of the first matching rune
// (rune-aware for non-ASCII); slicing gives the strpbrk tail.
package main
import (
"fmt"
"strings"
)
func main() {
s := "hello world"
p := strings.IndexAny(s, "lw")
fmt.Println(s[p:])
fmt.Println(p)
fmt.Println(strings.IndexAny("2026-11-25", "0123456789"))
fmt.Println(strings.IndexAny("abc", "xyz") < 0)
}
llo world 2 0 true
Java v27.0.1
// Hand-written scan: first index whose char belongs to the charset,
// then substring for the strpbrk tail.
public class Main {
static int firstOfAny(String s, String chars) { // strpbrk as an index
for (int i = 0; i < s.length(); i++)
if (chars.indexOf(s.charAt(i)) >= 0) return i;
return -1;
}
public static void main(String[] args) {
String s = "hello world";
int p = firstOfAny(s, "lw");
System.out.println(s.substring(p));
System.out.println(p);
System.out.println(firstOfAny("2026-11-25", "0123456789"));
System.out.println(firstOfAny("abc", "xyz") < 0);
}
}
llo world 2 0 true
JavaScript v26.13.1
// Hand-written scan: first index whose char belongs to the charset.
function firstOfAny(s, chars) { // strpbrk as an index (false when missing)
for (let i = 0; i < s.length; i++)
if (chars.includes(s[i])) return i;
return -1;
}
const s = "hello world";
const p = firstOfAny(s, "lw");
console.log(s.slice(p));
console.log(p);
console.log(firstOfAny("2026-11-25", "0123456789"));
console.log(firstOfAny("abc", "xyz") < 0);
llo world 2 0 true
PHP v8.6.0
// strpbrk - scan for the FIRST occurrence of ANY character from a charset.
// Returns the TAIL from that position (not an index); false when nothing
// matches; ValueError on an empty charset (8.0+).
var_dump(strpbrk("hello world", "lw")); // 'l' hits first (pos 2) -> "llo world"
var_dump(strpbrk("2026-11-25", "0123456789")); // first char already matches
var_dump(strpbrk("abc", "xyz") === false);
var_dump(strpbrk("a,b;c", ",;")); // ',' at 1 -> ",b;c"
string(9) "llo world" string(10) "2026-11-25" bool(true) string(4) ",b;c"
Python v3.15.2
# Hand-written scan via next(): the first index whose char is in the set.
def first_of_any(s, chars): # strpbrk as an index (-1 when missing)
return next((i for i, ch in enumerate(s) if ch in chars), -1)
s = "hello world"
p = first_of_any(s, "lw")
print(s[p:])
print(p)
print(first_of_any("2026-11-25", "0123456789"))
print(first_of_any("abc", "xyz") < 0)
llo world 2 0 True
Rust v1.99.1
// Iterator + position: find the first char contained in the charset set.
use std::collections::HashSet;
fn main() {
let chars: HashSet<char> = "lw".chars().collect();
let s = "hello world";
let p = s.chars().position(|c| chars.contains(&c)).unwrap();
println!("{}", &s[p..]);
println!("{}", p);
let digits: HashSet<char> = "0123456789".chars().collect();
println!("{}", "2026-11-25".chars().position(|c| digits.contains(&c)).unwrap());
let none: HashSet<char> = "xyz".chars().collect();
println!("{}", "abc".chars().position(|c| none.contains(&c)).is_none());
}
llo world 2 0 true
TypeScript v26.13.1
// Same hand-written scan as JavaScript.
function firstOfAny(s: string, chars: string): number {
for (let i = 0; i < s.length; i++)
if (chars.includes(s[i])) return i;
return -1;
}
const s: string = "hello world";
const p: number = firstOfAny(s, "lw");
console.log(s.slice(p));
console.log(p);
console.log(firstOfAny("2026-11-25", "0123456789"));
console.log(firstOfAny("abc", "xyz") < 0);
llo world 2 0 true
Language differences
C: strpbrk(3) is the C original (<string.h>) - same character-class scanning family as strspn/strcspn (see the strspn_strcspn entry): spn counts length, pbrk yields a pointer. C++: find_first_of is the direct builtin (no hand-writing needed); find_last_of is the right-to-left version. C#: IndexOfAny(char[]). Go: strings.IndexAny (rune-aware; non-ASCII charsets match per rune). Java/JavaScript/TypeScript/Python: no builtin - the idiomatic answer is a hand-written scan (exactly the minimal implementation shown - a double loop / generator / one-line iterator). PHP: the return value is the tail substring, not an index - unlike strcspn (length) and strpos (index); a PHP-specific convention inherited from C (whose strpbrk also returns a pointer, not an index); an empty $characters throws ValueError since 8.0 (previously E_WARNING + false); typical uses: finding the first delimiter or first invalid character. Rust: the chars().position(|c| set.contains(&c)) iterator idiom. Teaching anchor: the character-class scanning trio - "first occurrence" (strpbrk/IndexOfAny/find_first_of), "prefix span" (strspn), "prefix complement span" (strcspn); the divergence is WHAT is returned: pointer/tail (C/PHP), index (C#/Go/hand-written), bool plus slicing (the rest).