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.
  • StringPart[BioSequence["type","seq"],spec] will apply the operation to the string "seq" yielding BioSequence results.

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  (6)

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:

Extract single-letter sequences from a DNA sequence:

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:

Wolfram Research (2016), StringPart, Wolfram Language function, https://reference.wolfram.com/language/ref/StringPart.html (updated 2020).

Text

Wolfram Research (2016), StringPart, Wolfram Language function, https://reference.wolfram.com/language/ref/StringPart.html (updated 2020).

CMS

Wolfram Language. 2016. "StringPart." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2020. https://reference.wolfram.com/language/ref/StringPart.html.

APA

Wolfram Language. (2016). StringPart. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/StringPart.html

BibTeX

@misc{reference.wolfram_2024_stringpart, author="Wolfram Research", title="{StringPart}", year="2020", howpublished="\url{https://reference.wolfram.com/language/ref/StringPart.html}", note=[Accessed: 26-April-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_stringpart, organization={Wolfram Research}, title={StringPart}, year={2020}, url={https://reference.wolfram.com/language/ref/StringPart.html}, note=[Accessed: 26-April-2024 ]}