The Wolfram Language provides a variety of functions for manipulating strings. Most of these functions are based on viewing strings as a sequence of characters, and many of the functions are analogous to ones for manipulating lists.
StringTake and StringDrop are the analogs for strings of Take and Drop for lists. Like Take and Drop, they use standard Wolfram Language sequence specifications, so that, for example, negative numbers count character positions from the end of a string. Note that the first character of a string is taken to have position 1.
Here is a sample string.
This takes the first five characters from alpha.
Here is the fifth character in alpha.
This drops the characters 10 through 2, counting from the end of the string.
You can use StringPosition to find where a particular substring appears within a given string. StringPosition returns a list, each of whose elements corresponds to an occurrence of the substring. The elements consist of lists giving the starting and ending character positions for the substring. These lists are in the form used as sequence specifications in StringTake, StringDrop, and StringReplacePart.
This gives a list of the positions of the substring "abc".
This gives only the first occurrence of "abc".
This shows where both "abc" and "cd" appear. By default, overlaps are included.
This replaces all occurrences of the character a by the string XX.
This replaces abc by Y, and d by XXX.
The first occurrence of cde is not replaced because it overlaps with abc.
StringReplace scans a string from left to right, doing all the replacements it can, and then returning the resulting string. Sometimes, however, it is useful to see what all possible single replacements would give. You can get a list of all these results using StringReplaceList.
This gives a list of the results of replacing each a.
This shows the results of all possible single replacements.