StringPart¶
Status: Stable
documented, exercised by the test suite and/or worked examples, with no known limitations recorded.
Description¶
StringPart["string", n]
Gives the nth character in "string".
StringPart["string", {n1, n2, ...}]
Gives a list of the ni-th characters.
StringPart["string", m;;n;;s]
Gives characters m through n in steps of s.
StringPart[{s1, s2, ...}, spec]
Gives the list of results for each si. Negative indices count from the end.
Examples (14)¶
Every input below was run against the current Mathilda build and its output recorded.
Basic examples (11)¶
In[1]:= StringPart["abcdefghijklm", 6]
Out[1]= "f"
In[2]:= StringPart["abcdefghijklm", {1, 3, 5}]
Out[2]= {"a", "c", "e"}
In[3]:= StringPart["abcdefghijklm", -4]
Out[3]= "j"
In[4]:= StringPart["abcdefghijklm", 1;;6]
Out[4]= {"a", "b", "c", "d", "e", "f"}
In[5]:= StringPart["abcdefghijklm", 1;;-1;;2]
Out[5]= {"a", "c", "e", "g", "i", "k", "m"}
In[6]:= StringPart["abcdefghijklm", -1;;1;;-2]
Out[6]= {"m", "k", "i", "g", "e", "c", "a"}
In[7]:= StringPart[{"abcd", "efgh", "ijklm"}, 1]
Out[7]= {"a", "e", "i"}
In[8]:= StringPart[{"abcd", "efgh", "ijklm"}, {1, -1}]
Out[8]= {{"a", "d"}, {"e", "h"}, {"i", "m"}}
In[9]:= StringPart["abcdef", -3;;-1]
Out[9]= {"d", "e", "f"}
In[10]:= StringPart["abcde", 5;;1;;-1]
Out[10]= {"e", "d", "c", "b", "a"}
In[11]:= StringPart[x, 1]
Out[11]= StringPart[x, 1]
Applications (3)¶
The first character
The last character
A list of positions
Implementation notes¶
builtin_stringpart takes (string, spec) and indexes by byte (1-based; negative counts from the end via len + k + 1). The single-index path uses the helper stringpart_single, which bounds-checks k and returns a length-1 EXPR_STRING. A List spec maps stringpart_single over each index into a result List; a Span[m, n, s] spec resolves start/end/step (honouring All and negative endpoints), computes the element count, and emits the stepped characters as a List. When the first argument is itself a List of strings, the builtin recurses by constructing and evaluating an inner StringPart[si, spec] per element. Out-of-range or non-integer indices return NULL. ATTR_PROTECTED.
Attributes: Protected.
References¶
- Source:
src/strings/strings_init.c - Specification:
docs/spec/builtins/string-operations.md - Tests:
tests/test_string_review.c - Tests:
tests/test_strings.c
Notes & additional examples¶
Notes¶
StringPart indexes a string by byte, 1-based, with negative indices counting
from the end (len + k + 1). A single index gives a length-1 string; a list of
indices gives a list of characters, and a Span[m, n, s] gives the stepped
characters.
A first argument that is itself a list of strings is handled per element. An out-of-range or non-integer index leaves the call unevaluated.