C# Regex Trim, Remove Start and End Spaces

These C# examples remove start and end whitespace from strings with Regex.Replace. They trim strings with patterns.
Regex, trim. Regex can be used to trim whitespace. Trimming a string removes whitespace and leaves only the important contents. Several methods are available, each with pluses and minuses. We trim strings using Regex in the C# programming language.
Example. Regexes are very powerful, but also very powerful in giving me headaches. One problem I encountered was a multiple-line string. With Regex, you must indicate how you want the engine to treat newlines (\n).

Note: The two options we use are RegexOptions.Multiline and RegexOptions.Singleline.


Note 2: My requirements were to trim the beginning and ending whitespace from a medium size (maybe several kilobytes) string.

C# program that trims with Regex using System; using System.Text.RegularExpressions; class Program { static void Main() { // // Example string // string source = " Some text "; // // Use the ^ to always match at the start of the string. // Then, look through all WHITESPACE characters with \s // Use + to look through more than 1 characters // Then replace with an empty string. // source = Regex.Replace(source, @"^\s+", ""); // // The same as above, but with a $ on the end. // This requires that we match at the end. // source = Regex.Replace(source, @"\s+$", ""); Console.Write("["); Console.Write(source); Console.WriteLine("]"); } } Output [Some text]
Example 2. Readers have commented on compiled Regex objects. Compiled regexes do make a substantial performance improvement. My quick tests showed that for using two compiled regexes 1,000 times each was about 47% faster than not compiling them.
Fragment that shows compiled Regex: C# // // Use two precompiled Regexes. // Regex a1 = new Regex(@"^\s+", RegexOptions.Compiled); Regex a2 = new Regex(@"\s+$", RegexOptions.Compiled); foreach (object item in _collection) // Example loop. { // // Reuse the compiled regex objects over and over again. // string source = " Some text "; source = a1.Replace(source, ""); source = a2.Replace(source, ""); // compiled: 3620 }
Example 3. Here we look at another way you can trim strings using Regex. What if we could combine the two above regular expressions into a single one, and then compile that? Well, we certainly can do that, and I wrote the code.
Fragment that shows alternate syntax: C# string source = " Some text "; // // Use the alternate syntax "|" for combining both regexes. // source = Regex.Replace(source, @"^\s+|\s+$", ""); // // Same as before but with the two alternates switched. // source = Regex.Replace(source, @"\s+$|^\s+", ""); // ... we could compile all of these too. //
Discussion. We need a balance between development time and code size. If you need Trim with different requirements than the built-in methods, the Regex methods will be easier. If it is at all reasonable for you to use the built-in Trim, do so.TrimTrimEnd, TrimStart
Summary. We looked at some methods of trimming the starts and ends of strings using the System.Text.RegularExpressions namespace in the C# language. For my projects where performance is critical, I don't use Regex.

Note: Some programs focus on speed. But for many tasks, such as processing data in the background, Regex is ideal.

© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to
Dot Net Perls