The following method trims the given string of both white-space characters and control characters, and its rather fast!
The following pieces of code help figure out how two intervals overlap, and provides helper methods to represent them in a way that is useful for debugging purposes. This job is quite easy to accomplish, but since I’ve written it too many times from scratch, I put them in the following gist. The IntervalOverlapKinds.cs file defines the IntervalOverlapKinds enumeration. The interval detection is carried out in the static methods of the IntervalOverlap class.
Suppose that you intend to find a string or a token (i.e., whole word, instead of a substring) and change their color in some Windows Forms RichTextBox. The following code snippets will help. The FindStringAndSetColor method looks for instances of an arbitrary string in the contents of the RichTextBox, while the FindTokenAndSetColor method looks for whole words (tokens).