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. 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 S {displaystyle S} is a substring of S {displaystyle S} that occurs at the beginning of S {displaystyle S} . A suffix of a string S {displaystyle S} is a substring that occurs at the end of S {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', ''. A substring (or factor) of a string T = t 1 … t n {displaystyle T=t_{1}dots t_{n}} is a string T ^ = t 1 + i … t m + i {displaystyle {hat {T}}=t_{1+i}dots t_{m+i}} , where 0 ≤ i {displaystyle 0leq i} and m + i ≤ n {displaystyle m+ileq n} . A substring of a string is a prefix of a suffix of the string, and equivalently a suffix of a prefix. If T ^ {displaystyle {hat {T}}} is a substring of T {displaystyle T} , it is also a subsequence, which is a more general concept. Given a pattern P {displaystyle P} , you can find its occurrences in a string T {displaystyle T} with a string searching algorithm. Finding the longest string which is equal to a substring of two or more strings is known as the longest common substring problem. Example: The string ana is equal to substrings (and subsequences) of banana at two different offsets: In the mathematical literature, substrings are also called subwords (in America) or factors (in Europe). Not including the empty substring, the number of substrings of a string of length n {displaystyle n} where symbols only occur once, is the number of ways to choose two distinct places between symbols to start/end the substring. Including the very beginning and very end of the string, there are n + 1 {displaystyle n+1} such places. So there are ( n + 1 2 ) = n ( n + 1 ) 2 {displaystyle { binom {n+1}{2}}={ frac {n(n+1)}{2}}} non-empty substrings. A prefix of a string T = t 1 … t n {displaystyle T=t_{1}dots t_{n}} is a string T ^ = t 1 … t m {displaystyle {widehat {T}}=t_{1}dots t_{m}} , where m ≤ n {displaystyle mleq n} . A proper prefix of a string is not equal to the string itself ( 0 ≤ m < n {displaystyle 0leq m<n} ); some sources in addition restrict a proper prefix to be non-empty ( 0 < m < n {displaystyle 0<m<n} ). A prefix can be seen as a special case of a substring. Example: The string ban is equal to a prefix (and substring and subsequence) of the string banana: