
Truncate restricts the maximum length of strings. You want to truncate your C# string so that you have a substring of the first specified number of letters. This is useful for text in databases or in user interfaces where space is limited.
This C# article implements a truncate method. Truncate shortens strings that are too long.

First, the base class library in .NET does not provide a Truncate method on string. You have to use a conditional expression and either Substring or Remove. Looking in IL Disassembler, Remove simply calls into Substring, so the two custom methods next just call Substring directly.
IL Disassembler TutorialProgram that truncates strings [C#]
using System;
class Program
{
static void Main()
{
Console.WriteLine(StringTool.Truncate("Carrot", 3)); // Write first 3 characters
Console.WriteLine(StringTool.Truncate2("Carrot", 3)); // Same
Console.WriteLine(StringTool.Truncate("Wagstaff", 20)); // Write all characters
Console.WriteLine(StringTool.Truncate2("Wagstaff", 20)); // Same
}
}
/// <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));
}
}
Output
Car
Car
Wagstaff
Wagstaff
Description. The above code contains three methods, the Main entry point and two methods in the StringTool static class. The two methods truncate strings.
First Truncate method. The first truncation method, Truncate, first tests the length of the source string to see if it needs any processing at all. If the source is longer than the max length, we call Substring to get the first N characters. This copies the string.
Second Truncate method. The second truncation method, Truncate2, uses a nested Math.Min expression in a Substring call. In this method, the Substring method is always entered. This is inefficient if no processing is necessary.

I wanted to know if avoiding the Substring call entirely in cases where it is not needed would have an impact on performance. The result was that it does, and using the if statement before calling Substring is more efficient.
Benchmarked code [C#]
// This code was in one loop:
string a = StringTool.Truncate("Carrot", 3);
string b = StringTool.Truncate("Carrot", 10);
// This code was in another loop:
string a = StringTool.Truncate2("Carrot", 3);
string b = StringTool.Truncate2("Carrot", 10);
Notes on benchmark
100000000 iterations of the two loops.
Standard benchmark loops were used.
Truncate with if: 2753 ms [faster]
Truncate with Math.Min: 3463 msDescription. The truncate with if finished the test in 2753 ms, while the truncate with the Math.Min expression completed the test in 3463 ms. Therefore, the first Truncate is 20.50% faster on this test.

Here we saw two implementations of string Truncate in the C# language and validated that they have correct results. Also, we saw how avoiding the Substring call when it is not necessary is substantially faster.
String Type