StringTake¶
Status: Stable
documented, exercised by the test suite and/or worked examples, with no known limitations recorded.
Description¶
StringTake["string", n]
Gives a string containing the first n characters.
StringTake["string", -n]
Gives the last n characters.
StringTake["string", {n}]
Gives the nth character.
StringTake["string", {m, n}]
Gives characters m through n.
StringTake["string", {m, n, s}]
Gives characters m through n in steps of s.
StringTake["string", UpTo[n]]
Gives n characters, or as many as are available.
StringTake[{s1, s2, ...}, spec]
Gives the list of results for each si.
Examples¶
All examples below are verified against the current Mathilda build.
In[1]:= StringTake["abcdefghijklm", 6]
Out[1]= "abcdef"
In[2]:= StringTake["abcdefghijklm", -4]
Out[2]= "jklm"
In[3]:= StringTake["abcdefghijklm", {5, 10}]
Out[3]= "efghij"
In[4]:= StringTake["abcdefghijklm", {6}]
Out[4]= "f"
In[5]:= StringTake["abcdefghijklm", {1, -1, 2}]
Out[5]= "acegikm"
In[6]:= StringTake[{"abcdef", "stuv", "xyzw"}, -2]
Out[6]= {"ef", "uv", "zw"}
In[7]:= StringTake["abc", UpTo[4]]
Out[7]= "abc"
In[8]:= StringTake["abcdef", {-3, -1}]
Out[8]= "def"
Implementation notes¶
builtin_stringtake takes (string, spec) and slices by byte through the helper stringtake_substring (which memcpys str[start-1 .. end-1] into a fresh buffer). A positive integer n takes the first n bytes; negative -n takes the last n; UpTo[n] (detected by is_upto) clamps to the available length. A List spec selects: {n} a single character, {m, n} a range, and {m, n, s} a stepped range built byte-by-byte into a malloc'd buffer. Negative endpoints normalise as len + k + 1; out-of-range or non-integer specs return NULL. A first argument that is a List of strings is handled by recursively building and evaluating StringTake[si, spec] per element. ATTR_PROTECTED.
Attributes: Protected.
Implementation status¶
Stable — documented, exercised by the test suite and/or worked examples, with no known limitations recorded.
References¶
- Source:
src/picostrings.c - Specification:
docs/spec/builtins/string-operations.md