strspn / strcspn
strspn(string $string, string $characters, int $offset = 0, ?int $length = null): int — strcspn(string $string, string $characters, int $offset = 0, ?int $length = null): int
Character-set spans: strspn returns the length of the initial segment consisting entirely of characters in the set, strcspn the initial segment containing none of them. The purest C-lineage string functions, commonly used for input validation (strspn result equal to strlen means the whole string is legal).
C C23
#include <stdio.h>
#include <string.h>
int main(void) {
const char *s = "1997 was the year";
// strspn/strcspn are the C originals from <string.h>
printf("%zu\n", strspn(s, "0123456789")); // 4: initial segment all digits
printf("%zu\n", strcspn(s, "0123456789")); // 0: first char is already in the reject set
printf("%zu\n", strcspn(s, " ")); // 4: initial segment without any space
return 0;
}
4 0 4
C++ C++26
#include <iostream>
#include <string>
int main() {
std::string s = "1997 was the year";
std::string digits = "0123456789";
// no std::strspn equivalent on std::string — hand-written span loops
std::size_t spn = 0;
while (spn < s.size() && digits.find(s[spn]) != std::string::npos) ++spn;
std::size_t cspn = 0;
while (cspn < s.size() && digits.find(s[cspn]) == std::string::npos) ++cspn;
std::cout << spn << "\n"; // 4
std::cout << cspn << "\n"; // 0
std::cout << s.find(' ') << "\n"; // 4
return 0;
}
4 0 4
C# v11.0.100
string s = "1997 was the year";
string digits = "0123456789";
// no span functions — hand-written loops
int spn = 0;
while (spn < s.Length && digits.Contains(s[spn])) spn++;
int cspn = 0;
while (cspn < s.Length && !digits.Contains(s[cspn])) cspn++;
Console.WriteLine(spn); // 4
Console.WriteLine(cspn); // 0
Console.WriteLine(s.IndexOf(' ')); // 4
4 0 4
Go v1.27.4
package main
import (
"fmt"
"strings"
)
func main() {
s := "1997 was the year"
// no span functions — compose with IndexFunc (index of the first rune matching the predicate)
fmt.Println(strings.IndexFunc(s, func(r rune) bool { return r < '0' || r > '9' })) // 4: strspn equivalent
fmt.Println(strings.IndexFunc(s, func(r rune) bool { return r >= '0' && r <= '9' })) // 0: strcspn equivalent
fmt.Println(strings.IndexFunc(s, func(r rune) bool { return r == ' ' })) // 4
}
4 0 4
Java v27.0.1
public class Main {
public static void main(String[] args) {
String s = "1997 was the year";
String digits = "0123456789";
// no span functions — hand-written loops
int spn = 0;
while (spn < s.length() && digits.indexOf(s.charAt(spn)) >= 0) spn++;
int cspn = 0;
while (cspn < s.length() && digits.indexOf(s.charAt(cspn)) < 0) cspn++;
int cspnSpace = 0;
while (cspnSpace < s.length() && s.charAt(cspnSpace) != ' ') cspnSpace++;
System.out.println(spn); // 4
System.out.println(cspn); // 0
System.out.println(cspnSpace); // 4
}
}
4 0 4
JavaScript v26.13.1
const s = "1997 was the year"
const digits = "0123456789"
// no span functions — hand-written loops
let spn = 0
while (spn < s.length && digits.includes(s[spn])) spn++
let cspn = 0
while (cspn < s.length && !digits.includes(s[cspn])) cspn++
console.log(spn) // 4
console.log(cspn) // 0
console.log(s.indexOf(" ")) // 4
4 0 4
PHP v8.6.0
$s = "1997 was the year";
var_dump(strspn($s, "0123456789")); // 4: initial segment made only of digits
var_dump(strcspn($s, "0123456789")); // 0: first char is already in the reject set
var_dump(strcspn($s, " ")); // 4: initial segment without any space
int(4) int(0) int(4)
Python v3.15.2
s = "1997 was the year"
digits = "0123456789"
# no span functions — strspn via lstrip with a char set
print(len(s) - len(s.lstrip(digits))) # 4
# strcspn via a generator: index of the first char in the reject set
print(next((i for i, c in enumerate(s) if c in digits), len(s))) # 0
print(next((i for i, c in enumerate(s) if c == " "), len(s))) # 4
4 0 4
Rust v1.99.1
fn main() {
let s = "1997 was the year";
// no span functions — compose with find() plus a predicate
println!("{:?}", s.find(|c: char| !c.is_ascii_digit())); // Some(4): strspn equivalent
println!("{:?}", s.find(|c: char| c.is_ascii_digit())); // Some(0): strcspn equivalent
println!("{:?}", s.find(' ')); // Some(4)
}
Some(4) Some(0) Some(4)
TypeScript v26.13.1
const s: string = "1997 was the year";
const digits: string = "0123456789";
// no span functions — hand-written loops
let spn = 0;
while (spn < s.length && digits.includes(s[spn])) spn++;
let cspn = 0;
while (cspn < s.length && !digits.includes(s[cspn])) cspn++;
console.log(spn); // 4
console.log(cspn); // 0
console.log(s.indexOf(" ")); // 4
4 0 4
Language differences
C: strspn/strcspn are same-family functions in <string.h>; the classic validation idiom is strspn(s, legal-set) == strlen(s).
C++: no built-in span on std::string - hand-written loops.
C#: no built-in - hand-written loops.
Go: no built-in - compose with strings.IndexFunc plus a predicate (the example shows the equivalent).
Java: no built-in - hand-written loops.
JavaScript: no built-in - hand-written loops.
PHP: the second argument is a character SET, not a substring; strspn is the length of the initial segment entirely in the set and strcspn entirely out of it - the two answers complement each other (measured 4 and 0 in this entry).
Python: no built-in - strspn via the length difference of lstrip(char-set), strcspn via a generator finding the first rejected char.
Rust: no built-in - compose find() with a closure.
TypeScript: same as JavaScript.