Home

Search

C# TrimEnd and TrimStart

Use the TrimEnd and TrimStart methods on strings. TrimEnd removes trailing characters.

dot net perls

TrimEnd, TrimStart. TrimEnd removes ending characters from a string. Suppose we have a string with trailing punctuation. We can use TrimEnd to remove these characters.

Meanwhile, TrimStart removes from the start of a string. These methods are useful. And their effects are simple. They return copied strings.Trim

TrimEnd. This program loops through an array and trims its ending characters with TrimEnd. We use a string array. The results are printed to the screen.
Note: The string array "items" here is used to show the results of TrimEnd. The TrimEnd method itself receives chars, not strings.
ArrayChar
First: The example first creates a string array. Each string in the array is looped over with the foreach-loop.
Then: TrimEnd is called. It removes the question mark, period and comma from the string.
C# program that uses TrimEnd using System; class Program { static void Main() { // Our example string array. string[] items = new string[] { "Who's there?", "I am here..." }; // Loop and call TrimEnd. foreach (string item in items) { string trimmed = item.TrimEnd('?', '.', ','); Console.WriteLine(item); Console.WriteLine(" " + trimmed); } Console.ReadLine(); } } Output Who's there? Who's there I am here... I am here

Signature. Let us examine the TrimEnd method in the .NET Framework. TrimEnd receives a params argument. This allows you to use a special syntax to send characters to it.
Tip: You don't need an array, although you can use an array. The method is versatile.
Signature of TrimEnd method: C# string string.TrimEnd(params char[] trimChars) Removes all trailing occurrences of a set of characters specified...

Params. Sometimes you want to send TrimEnd an array you create. This allows you to easily change how you call TrimEnd. Use the syntax that is most clear for your program.
Note: You can send the method an entire array, or just several parameters separated by commas.
Params
Example of TrimEnd with many parameters: C# string t = s.TrimEnd('?', '.', ','); Example of TrimEnd with array: C# char[] c = new char[] { '?', '.', ',' }; string t = s.TrimEnd(c);

TrimStart. TrimStart removes leading characters. Sometimes strings have starting characters that are not needed. TrimStart removes as many characters of the specific values as required.
Note: We must provide a parameter to TrimStart, which contains the characters you want to Trim.
Tip: The characters can be anything. We do not need to use whitespace characters.
C# program that uses TrimStart using System; class Program { static void Main() { string text = "\t, again and again."; // Trim this string. char[] arr = new char[] { '\t', ',', ' ' }; // Trim these characters. text = text.TrimStart(arr); Console.WriteLine(text); } } Output "again and again."

Benchmark, trimming. Custom implementations of TrimEnd are faster than the version included in the .NET Framework. You can use a for-loop from the last index of the string.For
Version 1: We call TrimTrailingChars, an optimized method. In the method, we count characters, and call Substring().
Version 2: We call TrimEnd with a parameter array cached as a local variable. This avoids an allocation on each call.
Result: In .NET Core from 2020, TrimTrailingChars still performs faster than TrimEnd, but TrimEnd is faster than before.
Remove Punctuation
Tip: This could be a useful trick if you have a lot of trimming to do in an important program.
C# program that benchmarks optimized TrimEnd method using System; using System.Diagnostics; class Program { static string TrimTrailingChars(string value) { int removeLength = 0; for (int i = value.Length - 1; i >= 0; i--) { char let = value[i]; if (let == '?' || let == '!' || let == '.') { removeLength++; } else { break; } } if (removeLength > 0) { return value.Substring(0, value.Length - removeLength); } return value; } const int _max = 10000000; static void Main() { var paramArray = new char[] { '?', '!', '.' }; var s1 = Stopwatch.StartNew(); // Version 1: use optimized method. for (int i = 0; i < _max; i++) { var result = TrimTrailingChars("hello!?"); if (result == null) { return; } } s1.Stop(); var s2 = Stopwatch.StartNew(); // Version 2: use TrimEnd. for (int i = 0; i < _max; i++) { var result = "hello!?".TrimEnd(paramArray); if (result == null) { return; } } s2.Stop(); Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns")); Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns")); } } Output 13.54 ns Optimized method 15.85 ns TrimEnd

A summary. TrimEnd and TrimStart can be used with array parameters. They remove chars from the end and start of strings. They remove all characters specified until they can't remove one.

Home

© 2007-2020 sam allen. send bug reports to info@dotnetperls.com.