StringTakeDrop¶
Status: Stable
documented, exercised by the test suite and/or worked examples, with no known limitations recorded.
Description¶
StringTakeDrop["string", spec]
Gives {StringTake["string", spec], StringDrop["string", spec]}.
StringTakeDrop[{s1, s2, ...}, spec]
Gives one pair for each of the si.
Examples (11)¶
Every input below was run against the current Mathilda build and its output recorded.
Basic examples (3)¶
In[1]:= StringTakeDrop["abcdef", {2, 4}]
Out[1]= {"bcd", "aef"}
In[2]:= StringTakeDrop["abcdef", 2]
Out[2]= {"ab", "cdef"}
In[3]:= StringTakeDrop[{"abcd", "wxyz"}, -1]
Out[3]= {{"d", "abc"}, {"z", "wxy"}}
Applications (8)¶
The last two characters
A stepped span splits odd and even positions
Clamping with UpTo instead of failing
Positions are characters
One pair per string
Out of range: StringTakeDrop::take
Algorithm¶
stringtakedrop.c - StringTakeDrop["string", spec].
Gives {StringTake["string", spec], StringDrop["string", spec]} for one standard sequence specification (n, -n, {n}, {m, n}, {m, n, s}, UpTo[n], All, None), sharing StringTake's / StringDrop's scalar kernels. A list of strings gives one pair per string. Positions count characters.
Diagnostics are StringTakeDrop's own (Mathematica): ::strse for a subject that is not a string or list of strings, ::seqs for an invalid spec, ::take for out-of-range positions -- all leaving the call unevaluated.
Implementation notes¶
Algorithm. builtin_stringtakedrop requires exactly two arguments (else ::argr via strings_argr_error). The subject must be a String or a List of strings (else ::strse), and the spec is parsed by strings_seq_parse into one StrSeq — n, -n, {n}, {m, n}, {m, n, s}, UpTo[n], All, None (else ::seqs). Before producing anything, the spec is resolved against every subject with strings_chars_init and strings_seq_resolve, so an out-of-range position reports StringTakeDrop::take rather than the underlying kernels' own messages. Each pair is then {strings_take_seq(s, q), strings_drop_seq(s, q)} — exactly the scalar kernels of StringTake and StringDrop — so the two halves always partition the string's characters.
Data structures. StrSeq (a kind tag plus m, n, step s) and a StrChars view that indexes character positions over the UTF-8 bytes, built only for the prefix the spec needs (strings_seq_prefix).
Complexity / limits. O(B) per subject for the take and the drop. Only a single sequence spec is accepted (no list of specs), matching Mathematica.
Attributes: Protected.
References¶
See also: StringTake
- Source:
src/strings/stringtakedrop.c - Specification:
docs/spec/builtins/string-operations.md - Tests:
tests/test_string_review.c
Notes & additional examples¶
Notes¶
- Equivalent to
{StringTake[s, spec], StringDrop[s, spec]}and built on the same scalar kernels (strings_take_seq/strings_drop_seq), so the two halves always partition the characters ofs. - Accepts a single sequence spec:
n,-n,{n},{m, n},{m, n, s},UpTo[n],All,None. Positions are validated against every string first, so an out-of-range spec reportsStringTakeDrop::take(notStringTake::take). - Messages:
::strse(subject),::seqs(spec),::take(range),::argr(argument count). Attributes{Protected}. - See also:
StringTake,StringDrop,TakeDrop.