Home
C#
String Truncate
This page was last reviewed on Jul 6, 2021.
Dot Net Perls
Truncate. This C# method restricts the maximum length of strings. We truncate a string so that it has a substring of the first specified number of letters.
Shows a method
Truncate is useful for text in databases or in user interfaces where space is limited. It can be helpful when justifying text to the right as well.
String Substring
An example. The string type does not provide a Truncate method. We must use a conditional expression and either Substring or Remove.
Tip Looking in IL Disassembler, Remove calls into Substring. So these custom methods directly call Substring.
String Remove
Detail If the source is longer than the max length, we call Substring to get the first N characters. This copies the string.
Detail Use a nested Math.Min expression in a Substring call. In this method, the Substring method is always entered.
Math.Max, Math.Min
Info By always entering the Substring method, we may create string copies that are not needed.
Shows a method
using System; class Program { static void Main() { string result = StringTool.Truncate("Carrot", 3); string result2 = StringTool.Truncate2("Carrot", 3); Console.WriteLine(result); Console.WriteLine(result2); result = StringTool.Truncate("Computer", 20); result2 = StringTool.Truncate2("Computer", 20); Console.WriteLine(result); Console.WriteLine(result2); } } /// <summary> /// Custom string utility methods. /// </summary> public static class StringTool { /// <summary> /// Get a substring of the first N characters. /// </summary> public static string Truncate(string source, int length) { if (source.Length > length) { source = source.Substring(0, length); } return source; } /// <summary> /// Get a substring of the first N characters. [Slow] /// </summary> public static string Truncate2(string source, int length) { return source.Substring(0, Math.Min(length, source.Length)); } }
Car Car Computer Computer
Performance. I wanted to know if avoiding Substring() when it is not needed would have an impact on performance. And I found using the if-statement before calling Substring is faster.
if
Version 1 This code calls Truncate, which uses an if-statement to avoid calling Substring if the truncation does not need to occur.
Benchmark
Version 2 This code always uses substring, but uses Math.Min to ensure we do not take a substring that is too long.
Result Truncate, which avoids calling Substring(), is faster. It avoids a string method call in the benchmark.
using System; using System.Diagnostics; class Program { public static class StringTool { public static string Truncate(string source, int length) { if (source.Length > length) { source = source.Substring(0, length); } return source; } public static string Truncate2(string source, int length) { return source.Substring(0, Math.Min(length, source.Length)); } } const int _max = 100000000; static void Main() { var s1 = Stopwatch.StartNew(); // Version 1: use Truncate. for (int i = 0; i < _max; i++) { string a = StringTool.Truncate("Carrot", 3); string b = StringTool.Truncate("Carrot", 10); } s1.Stop(); // Version 2: use Truncate2. var s2 = Stopwatch.StartNew(); for (int i = 0; i < _max; i++) { string a = StringTool.Truncate2("Carrot", 3); string b = StringTool.Truncate2("Carrot", 10); } 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")); } }
14.18 ns Truncate 15.28 ns Truncate2
A summary. Here we saw 2 implementations of string Truncate. We validated that they have correct results. Avoiding the Substring call when it is not necessary is faster.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Jul 6, 2021 (edit).
Home
Changes
© 2007-2024 Sam Allen.