StringPart

StringPart["string",n]

gives the n^(th) character in "string".

StringPart["string",{n1,n2,}]

gives a list of the ni^(th) characters in "string".

StringPart["string",m;;n;;s]

gives a list of the characters in "string" from the m^(th) through the n^(th) in steps of s.

StringPart[{s1,s2,},spec]

gives the list of results for each of the si.

Details

  • StringPart["string",-n] counts from the end to give the n^(th)-to-last character in "string".
  • StringPart gives each character as a string.
  • In StringPart["string",m;;n;;s], m, n, and/or s can be negative.

Examples

open allclose all

Basic Examples  (3)

Extract the sixth character in a string:

Use InputForm to show quotes:

Extract several characters from a string:

Extract a character from the end of a string:

Scope  (5)

Obtain the first six characters of a string:

Obtain a list of every other character:

Obtain a list of every other character, starting from the end of the string:

Get the first character of each of several strings:

Find the first and last characters:

StringPart works with special characters:

Properties & Relations  (7)

StringPart[str,{}] returns the empty list:

StringPart works like applying Part to the list of characters:

The characters in the list returned by StringPart can be turned into a string using StringJoin:

StringPart[str,n] is effectively equivalent to StringTake[str,{n}]:

StringPart[str,n;;m;;s] returns a list of characters:

StringTake[str,{n,m,s}] returns the same characters as a single string:

StringPart[str,All] is effectively equivalent to Characters[str]:

StringPart operates on the raw characters in a string:

Possible Issues  (1)

StringPosition gives results in a form suitable for StringTake, not StringPart:

Introduced in 2016
 (11.0)