Trim
The C# Trim()
method eliminates leading and trailing whitespace. Often we need to remove whitespace from the beginning or ending of a string
—Trim
helps with this.
But Trim()
is not limited to just spaces. It removes any characters specified. Its behavior changes when we pass arguments to it.
Here we remove spaces from the beginning and end of a string
. When we call Trim
on a string
, it copies the string
and returns a modified version as the copy.
string
, which (like all strings) is immutable. The original is not changed when we call Trim
.Trim()
can be used with no parameters. In this case it uses a default set of whitespace characters.Console.WriteLine
statement prints the copied string
that has no spaces at the beginning or end.using System; // Step 1: input string. string value = " a line "; // Step 2: call Trim instance method. // ... This returns a new string copy. value = value.Trim(); // Step 3: write to screen. Console.WriteLine("TRIMMED: [{0}]", value);TRIMMED: [a line]
We can remove newlines from strings with Trim
. The Trim
method will remove both UNIX style line breaks and Windows style line breaks.
string
, and we print it to the Console
with Console.WriteLine
, surrounding it with square brackets.Trim
—it copies the input string, modifies the copy, and then returns a new copy.using System; // Part 1: display the input string. string value = "This is an example string.\r\n\r\n"; Console.WriteLine("[{0}]", value); // Part 2: trim the line breaks. value = value.Trim(); Console.WriteLine("[{0}]", value);[This is an example string. ] [This is an example string.]
We next trim each line from a file. This works well with StreamReader
or other File methods. The example here demonstrates Trim
on each line in the file.
File.ReadAllLines
. Then it loops through the lines using foreach
.using System; using System.IO; foreach (string line in File.ReadAllLines("file.txt")) { Console.WriteLine("[" + line + "]"); string trimmed = line.Trim(); Console.WriteLine("[" + trimmed + "]"); } // In the loop we can use both strings. // ... No string is changed: Trim() copies the strings.[This file ] [This file] [Has some ] [Has some] [Whitespace you don't want ] [Whitespace you don't want]
I tested how Trim
performs on different strings. For example, I benchmarked Trim
on a string
that has characters to be trimmed, against a string
that doesn't need changing.
Trim
takes more time when it has more characters to remove from the source string
.Trim()
iterates through the characters until no more trimming can be done. No numbers are available in this document.There are other ways you will need to change or replace the whitespace in strings. For example, you may need to change whitespace in the middle of strings, not just on the end.
Punctuation is another common character type we need to trim. This can be challenging with just the Trim
method—a custom array is required.
Trimming strings is commonly done—it removes leading and trailing whitespace. Trim
, unlike TrimEnd
and TrimStart
, acts on both the start and end in a single call.