# Substring

A substring is a contiguous sequence of characters within a string. For instance, "the best of" is a substring of "It was the best of times". This is not to be confused with subsequence, which is a generalization of substring. For example, "Itwastimes" is a subsequence of "It was the best of times", but not a substring.

Prefix and suffix are special cases of substring. A prefix of a string ${\displaystyle S}$ is a substring of ${\displaystyle S}$ that occurs at the beginning of ${\displaystyle S}$. A suffix of a string ${\displaystyle S}$ is a substring that occurs at the end of ${\displaystyle S}$.

The list of all substrings of the string "apple" would be "apple", "appl", "pple", "app", "ppl", "ple", "ap", "pp", "pl", "le", "a", "p", "l", "e", "".

## Substring

A string ${\displaystyle u}$ is a substring (or factor)[1] of a string ${\displaystyle t}$ if there exists two strings ${\displaystyle p}$ and ${\displaystyle s}$ such that ${\displaystyle t=pus}$. In particular, the empty string is a substring of every string.

Example: The string ${\displaystyle u=}$`ana` is equal to substrings (and subsequences) of ${\displaystyle t=}$`banana` at two different offsets:

```banana
|||||
ana||
|||
ana```

The first occurrence is obtained with ${\displaystyle p=}$`b` and