DigitQ¶
Status: Stable
documented, exercised by the test suite and/or worked examples, with no known limitations recorded.
Description¶
DigitQ["string"]
Gives True if every character of "string" is a digit 0-9, and False otherwise. DigitQ[""] is True.
Examples (13)¶
Every input below was run against the current Mathilda build and its output recorded.
Basic examples (4)¶
In[1]:= LetterQ["αβγ"]
Out[1]= True
In[2]:= DigitQ["\:0662"]
Out[2]= False
In[3]:= UpperCaseQ["ABC"]
Out[3]= True
In[4]:= LowerCaseQ["abc1"]
Out[4]= False
Applications (9)¶
A sign is not a digit
Nor is a decimal point
Arabic-Indic digits: DigitQ is ASCII-only
While the pattern DigitCharacter accepts them
Vacuously True
A number is not a string: DigitQ::string
Implementation notes¶
Algorithm. builtin_digitq calls charq_builtin(res, "DigitQ", CQ_DIGIT), which requires one String argument (otherwise DigitQ::string via strings_arg_message, unevaluated), then decodes the UTF-8 code points one at a time with u8_decode and returns False at the first one for which char_ok(CQ_DIGIT, cp) fails — so the scan short-circuits — and True if none does (vacuously for ""). char_ok answers ASCII code points inline (0–9) and returns 0 for every non-ASCII code point without consulting any table — DigitQ is ASCII-only, as in Mathematica. Inside a string pattern, predicate_class in string_pattern.c translates _?DigitQ to the PCRE class [[:digit:]] instead of emitting a per-character callout.
Data structures. None beyond the decoder: the test is a pure range check on the code point.
Complexity / limits. O(L) for an ASCII string; any non-ASCII character fails immediately. Note the asymmetry with the string pattern DigitCharacter, which (compiled with PCRE2 UCP) matches every Unicode decimal digit. Characters are code points, not grapheme clusters. Not Listable; no packed-array or Compile[] surface (the argument is a String).
Attributes: Protected.
References¶
See also: LetterQ, UpperCaseQ, LowerCaseQ
- The Unicode Consortium, The Unicode Standard, Version 16.0 (2024), §4.5 General Category, and the Unicode Character Database (UnicodeData.txt).
- Source:
src/strings/stringcase.c - Specification:
docs/spec/builtins/string-operations.md - Tests:
tests/test_string_review.c
Notes & additional examples¶
Notes¶
Trueexactly when every character is an ASCII digit0–9, as in Mathematica — deliberately narrower than the string patternDigitCharacter, which matches any Unicode decimal digit.Truefor"". A non-string emitsDigitQ::stringand stays unevaluated; notListable.DigitQis a test on text, not a number test: useIntegerQ/NumberQfor expressions.- See also:
DigitCharacter,LetterQ,ToExpression.