ctype_digit
ctype_digit(mixed $text): bool
检查字符串中每个字符是否都是十进制数字 0-9(ASCII 窄语义),是 Ctype 字符类型检查家族的入口。
C C23
#include <ctype.h>
#include <stdio.h>
static int all_digits(const char *s) {
for (; *s != '\0'; s++) {
if (!isdigit((unsigned char)*s)) return 0;
}
return 1;
}
int main(void) {
printf("%d\n", all_digits("123"));
printf("%d\n", all_digits("12.3"));
/* isdigit 返回「非零即真」:显式与 0 比较;
(unsigned char) 强转避免负值 char 的未定义行为 */
printf("%d\n", isdigit('7') != 0);
return 0;
}
1 0 1
C++ C++26
#include <cctype>
#include <iostream>
#include <string>
// isdigit 返回「非零即真」:显式与 0 比较;
// 强转避免负值 char 的未定义行为
static bool allDigits(const std::string& s) {
for (char ch : s) {
if (!std::isdigit(static_cast<unsigned char>(ch))) return false;
}
return true;
}
int main() {
std::cout << std::boolalpha;
std::cout << allDigits("123") << "\n";
std::cout << allDigits("12.3") << "\n";
std::cout << (std::isdigit('7') != 0) << "\n";
return 0;
}
true false true
C# v11.0.100
// char.IsDigit 是 Unicode 宽语义:全角数字也算数字
static bool AllDigits(string s)
{
foreach (char c in s)
{
if (!char.IsDigit(c)) return false;
}
return true;
}
Console.WriteLine(AllDigits("123"));
Console.WriteLine(AllDigits("12.3"));
Console.WriteLine(AllDigits("123")); // true - Unicode 宽语义
Console.WriteLine(double.TryParse("12.3", out _)); // 数值字符串侧
True False True True
Go v1.27.4
package main
import (
"fmt"
"strconv"
"unicode"
)
func main() {
allDigits := func(s string) bool {
for _, r := range s {
if !unicode.IsDigit(r) {
return false
}
}
return true
}
fmt.Println(allDigits("123"))
fmt.Println(allDigits("12.3"))
fmt.Println(allDigits("123")) // unicode.IsDigit 是 Unicode 宽语义:true
_, err := strconv.ParseFloat("12.3", 64)
fmt.Println(err == nil)
}
true false true true
Java v27.0.1
// Character.isDigit 与 Python/Go 一样是 Unicode 宽语义:全角数字也算
class Main {
static boolean allDigits(String s) {
for (int i = 0; i < s.length(); i++) {
if (!Character.isDigit(s.charAt(i))) return false;
}
return true;
}
public static void main(String[] args) {
System.out.println(allDigits("123"));
System.out.println(allDigits("12.3"));
System.out.println(allDigits("123")); // true - Unicode 宽语义
try {
Double.parseDouble("12.3");
System.out.println(true);
} catch (NumberFormatException e) {
System.out.println(false);
}
}
}
true false true true
JavaScript v26.13.1
// 不带 u 标志的 /^\d+$/ 只匹配 ASCII 0-9——与 PHP ctype_digit 一致的窄语义
console.log(/^\d+$/.test("123"));
console.log(/^\d+$/.test("12.3"));
console.log(/^\d+$/.test("123")); // false - 全角不是 \d
// 数值层面的检查(is_numeric 的一侧)
console.log(Number.isFinite(Number("12.3")));
console.log(Number.isInteger("123")); // false - 字符串不是整数
true false false true false
PHP v8.6.0
// ctype_digit:每个字符都是 ASCII 十进制数字 0-9
var_dump(ctype_digit("123"));
var_dump(ctype_digit("12.3")); // '.' 不是数字字符
var_dump(ctype_digit("123")); // 全角数字是多字节字符,不在 ASCII 内
// 对比 is_numeric:数值字符串语义(允许正负号、小数点、科学计数法)
var_dump(is_numeric("12.3"));
var_dump(is_numeric(123));
bool(true) bool(false) bool(false) bool(true) bool(true)
Python v3.15.2
# str.isdigit 是 Unicode 语义:全角数字也算数字!
# 与 PHP ctype_digit(窄 ASCII)不同——好教学点
print("123".isdigit())
print("12.3".isdigit())
print("123".isdigit()) # True - 教学点
# 数值字符串语义(is_numeric 的一侧):尝试解析
def is_numeric_string(s):
try:
float(s)
return True
except ValueError:
return False
print(is_numeric_string("12.3"))
print(is_numeric_string("123"))
True False True True True
Rust v1.99.1
// char::is_ascii_digit 是窄 ASCII 检查(对应 PHP ctype_digit);
// 全角数字是多字节字符,不在 ASCII 内
fn all_ascii_digits(s: &str) -> bool {
s.chars().all(|c| c.is_ascii_digit())
}
fn main() {
println!("{}", all_ascii_digits("123"));
println!("{}", all_ascii_digits("12.3"));
println!("{}", all_ascii_digits("123"));
println!("{}", "12.3".parse::<f64>().is_ok()); // 数值字符串侧
}
true false false true
TypeScript v26.13.1
// 与 JS 相同的运行时正则;TS 类型把「"123" 还是 123」的问题移到编译期——
// string 参数不可能混进数字
function isDigitString(s: string): boolean {
return /^\d+$/.test(s); // 窄 ASCII 语义
}
console.log(isDigitString("123"));
console.log(isDigitString("12.3"));
console.log(isDigitString("123"));
console.log(Number.isFinite(Number("12.3")));
true false false true
语言差异说明
C:isdigit(3) 是窄 ASCII 语义(PHP Ctype 家族的祖宗);返回「非零即真」要显式 != 0;参数必须能表示为 unsigned char 或 EOF——传负值 char 是经典 UB,示例的强转就是为此。
C++:std::isdigit 同 C(窄 ASCII + 同样的 UB 陷阱 + cast);C++ 侧还有 std::isdigit(std::locale) 本地化版本,但默认 locale 仍是窄语义。
C#:char.IsDigit 是 Unicode 宽语义(Nd 类别,全角数字为 true);char.IsAsciiDigit(.NET 7+)才是窄 ASCII 对应物。
Go:unicode.IsDigit 是 Unicode 宽语义(全角 true);窄语义用 c >= '0' && c <= '9' 或 strconv.ParseInt 判断。
Java:Character.isDigit 同为 Unicode 宽语义(Nd 类别)——与 PHP ctype_digit 语义相反方向的典型案例。
JavaScript:/^\d+$/(无 u 标志)是窄 ASCII;\d 加 u 标志也不含全角——全角要用 \p{Nd}+u(宽语义开关在正则语法里)。
PHP:Ctype 家族一共有 ctype_alnum/alpha/cntrl/digit/graph/lower/print/punct/space/upper/xdigit 十二个函数,语义统一为窄 ASCII;与 is_numeric 的核心区别:ctype 只看「字符是不是数字」,is_numeric 看「整体是不是数值」(接受正负号、小数点、科学计数法,也接受真正的 int/float)——"12.3" 两者结果相反(示例)。
Python:str.isdigit() 是 Unicode 宽语义——'123'.isdigit() 为 True(与 PHP 结果相反,本条目最大教学点);此外 isdecimal(严格十进制)比 isdigit 更窄,isnumeric 又更宽(含汉字数字「三」)。
Rust:char::is_ascii_digit 是窄 ASCII(对应 PHP);宽语义用 char::is_numeric(Unicode Nd/Nl/No 全算,比 PHP 宽得多)。
TypeScript:运行时同 JavaScript(窄 \d);类型系统的字面量收窄把「字符串数字 vs 数字」问题前移到编译期。