Skip to content

StringTake ​

A substring of s — the first/last n characters, or an explicit 1-based range.

✓ checked against that system · ! checked, with disagreements (hover a chip)Wolfram LanguageStringTake✓
StringTake(s, n)the first n characters of s; the last |n| if n is negative
StringTake(s, {n})the nth character alone, as a length-1 string
StringTake(s, {m, n})characters m through n inclusive, 1-based, negative counting from the end
Typed signatures
TypePackageApplies
(string, integer | list<integer>) -> stringcombinatoricsalways

Domain: Collections

Details
  • Positions and lengths count Unicode code points — see StringLength.

Examples

Basic #
live ↗
InStringTake(hello world,5)\mathrm{StringTake}(\text{\text{hello world}}, 5)Outhello\text{hello}

The first six characters

live ↗
InStringTake(abcdefghijklm,6)\mathrm{StringTake}(\text{abcdefghijklm}, 6)Outabcdef\text{abcdef}

A negative count takes from the end

live ↗
InStringTake(abcdefghijklm,−4)\mathrm{StringTake}(\text{abcdefghijklm}, -4)Outjklm\text{jklm}

A pair gives an inclusive range of positions

live ↗
InStringTake(abcdefghijklm,[5,10])\mathrm{StringTake}(\text{abcdefghijklm}, \bigl\lbrack5, 10\bigr\rbrack)Outefghij\text{efghij}

The string is shorter than 5, so the call stays unevaluated, as in Wolfram

live ↗
InStringTake(hi,5)\mathrm{StringTake}(\text{hi}, 5)OutStringTake(hi,5)\mathrm{StringTake}(\text{hi}, 5)
Scope #
live ↗
InStringTake(hello world,−5)\mathrm{StringTake}(\text{\text{hello world}}, -5)Outworld\text{world}
live ↗
InStringTake(hello,[2])\mathrm{StringTake}(\text{hello}, \bigl\lbrack2\bigr\rbrack)Oute\text{e}
live ↗
InStringTake(hello world,[1,5])\mathrm{StringTake}(\text{\text{hello world}}, \bigl\lbrack1, 5\bigr\rbrack)Outhello\text{hello}

-7 counts back from the end, landing on the same range as {1, 5}

live ↗
InStringTake(hello world,[1,−7])\mathrm{StringTake}(\text{\text{hello world}}, \bigl\lbrack1, -7\bigr\rbrack)Outhello\text{hello}

A one-element list takes just that character

live ↗
InStringTake(abcdefghijklm,[6])\mathrm{StringTake}(\text{abcdefghijklm}, \bigl\lbrack6\bigr\rbrack)Outf\text{f}
Properties #

A newline counts as one character

live ↗
InStringTake(abc def,5)\mathrm{StringTake}(\text{abc\char"000A{}def}, 5)Outabc d\text{abc\char"000A{}d}

Range ends may be negative, counting from the end

live ↗
InStringTake(abcdefghijklm,[5,−4])\mathrm{StringTake}(\text{abcdefghijklm}, \bigl\lbrack5, -4\bigr\rbrack)Outefghij\text{efghij}
About
Examples12
Checked againstWolfram Language 12 agree
CrosswalkWolfram Language

See also: StringLength