How do I truncate a .NET string?

C#.NetStringTruncate

C# Problem Overview


I would like to truncate a string such that its length is not longer than a given value. I am writing to a database table and want to ensure that the values I write meet the constraint of the column's datatype.

For instance, it would be nice if I could write the following:

string NormalizeLength(string value, int maxLength)
{
    return value.Substring(0, maxLength);
}

Unfortunately, this raises an exception because maxLength generally exceeds the boundaries of the string value. Of course, I could write a function like the following, but I was hoping that something like this already exists.

string NormalizeLength(string value, int maxLength)
{
    return value.Length <= maxLength ? value : value.Substring(0, maxLength);
} 

Where is the elusive API that performs this task? Is there one?

C# Solutions


Solution 1 - C#

There isn't a Truncate() method on string, unfortunately. You have to write this kind of logic yourself. What you can do, however, is wrap this in an extension method so you don't have to duplicate it everywhere:

public static class StringExt
{
    public static string Truncate(this string value, int maxLength)
    {
        if (string.IsNullOrEmpty(value)) return value;
        return value.Length <= maxLength ? value : value.Substring(0, maxLength); 
    }
}

Now we can write:

var someString = "...";
someString = someString.Truncate(2);
2021-09-17 Alternative with suffix and c#8 nullable reference types.
public static class StringExt
{
    public static string? Truncate(this string? value, int maxLength, string truncationSuffix = "…")
    {
        return value?.Length > maxLength
            ? value.Substring(0, maxLength) + truncationSuffix
            : value;
    }
}

To write:

"abc".Truncate(2);          // "ab…"
"abc".Truncate(3);          // "abc"
((string)null).Truncate(3); // null

Solution 2 - C#

Or instead of the ternary operator, you could use Math.min

public static class StringExt
{
    public static string Truncate( this string value, int maxLength )
    {
        if (string.IsNullOrEmpty(value)) { return value; }

        return value.Substring(0, Math.Min(value.Length, maxLength));
    }
}

Solution 3 - C#

I figured I would throw in my implementation since I believe it covers all of the cases that have been touched on by the others and does so in a concise way that is still readable.

public static string Truncate(this string value, int maxLength)
{
    if (!string.IsNullOrEmpty(value) && value.Length > maxLength)
    {
        return value.Substring(0, maxLength);
    }

    return value;
}

This solution mainly builds upon the Ray's solution and opens up the method for use as an extension method by using the this keyword just as LBushkin does in his solution.

Solution 4 - C#

Because performance testing is fun: (using linqpad extension methods)

var val = string.Concat(Enumerable.Range(0, 50).Select(i => i % 10));

foreach(var limit in new[] { 10, 25, 44, 64 })
	new Perf<string> {
		{ "newstring" + limit, n => new string(val.Take(limit).ToArray()) },
		{ "concat" + limit, n => string.Concat(val.Take(limit)) },
		{ "truncate" + limit, n => val.Substring(0, Math.Min(val.Length, limit)) },
		{ "smart-trunc" + limit, n => val.Length <= limit ? val : val.Substring(0, limit) },
		{ "stringbuilder" + limit, n => new StringBuilder(val, 0, Math.Min(val.Length, limit), limit).ToString() },
	}.Vs();

The truncate method was "significantly" faster. #microoptimization

Early > * truncate10 5788 ticks elapsed (0.5788 ms) [in 10K reps, 5.788E-05 ms per] > * smart-trunc10 8206 ticks elapsed (0.8206 ms) [in 10K reps, 8.206E-05 ms per] > * stringbuilder10 10557 ticks elapsed (1.0557 ms) [in 10K reps, 0.00010557 ms per] > * concat10 45495 ticks elapsed (4.5495 ms) [in 10K reps, 0.00045495 ms per] > * newstring10 72535 ticks elapsed (7.2535 ms) [in 10K reps, 0.00072535 ms per]

Late > * truncate44 8835 ticks elapsed (0.8835 ms) [in 10K reps, 8.835E-05 ms per] > * stringbuilder44 13106 ticks elapsed (1.3106 ms) [in 10K reps, 0.00013106 ms per] > * smart-trunc44 14821 ticks elapsed (1.4821 ms) [in 10K reps, 0.00014821 ms per] > * newstring44 144324 ticks elapsed (14.4324 ms) [in 10K reps, 0.00144324 ms per] > * concat44 174610 ticks elapsed (17.461 ms) [in 10K reps, 0.0017461 ms per]

Too Long > * smart-trunc64 6944 ticks elapsed (0.6944 ms) [in 10K reps, 6.944E-05 ms per] > * truncate64 7686 ticks elapsed (0.7686 ms) [in 10K reps, 7.686E-05 ms per] > * stringbuilder64 13314 ticks elapsed (1.3314 ms) [in 10K reps, 0.00013314 ms per] > * newstring64 177481 ticks elapsed (17.7481 ms) [in 10K reps, 0.00177481 ms per] > * concat64 241601 ticks elapsed (24.1601 ms) [in 10K reps, 0.00241601 ms per]

Solution 5 - C#

In .NET 4.0 you can use the Take method:

string.Concat(myString.Take(maxLength));

Not tested for efficiency!

Solution 6 - C#

You could use LINQ... it eliminates the need to check string length. Admittedly maybe not the most efficient, but it's fun.

string result = string.Join("", value.Take(maxLength)); // .NET 4 Join

or

string result = new string(value.Take(maxLength).ToArray());

Solution 7 - C#

Another solution:

return input.Substring(0, Math.Min(input.Length, maxLength));

Solution 8 - C#

I did mine in one line sort of like this

value = value.Length > 1000 ? value.Substring(0, 1000) : value;

Solution 9 - C#

The .NET Framework has an API to truncate a string like this:

Microsoft.VisualBasic.Strings.Left(string, int);

But in a C# app you'll probably prefer to roll your own than taking a dependency on Microsoft.VisualBasic.dll, whose main raison d'etre is backwards compatibility.

Solution 10 - C#

Seems no one has posted this yet:

public static class StringExt
{
    public static string Truncate(this string s, int maxLength)
    {
        return s != null && s.Length > maxLength ? s.Substring(0, maxLength) : s;
    }
}

Using the && operator makes it marginally better than the accepted answer.

Solution 11 - C#

In C# 8 the new Ranges feature can be used...

value = value[..Math.Min(30, value.Length)];

    

Solution 12 - C#

I know this is an old question, but here is a nice solution:

public static string Truncate(this string text, int maxLength, string suffix = "...")
{
    string str = text;
    if (maxLength > 0)
    {
        int length = maxLength - suffix.Length;
        if (length <= 0)
        {
            return str;
        }
        if ((text != null) && (text.Length > maxLength))
        {
            return (text.Substring(0, length).TrimEnd(new char[0]) + suffix);
        }
    }
    return str;
}

var myString = "hello world"
var myTruncatedString = myString.Truncate(4);

Returns: hello...

Solution 13 - C#

Why not:

string NormalizeLength(string value, int maxLength)
{
    //check String.IsNullOrEmpty(value) and act on it. 
    return value.PadRight(maxLength).Substring(0, maxLength);
}

i.e. in the event value.Length < maxLength pad spaces to the end or truncate the excess.

Solution 14 - C#

Still no Truncate method in 2016 for C# strings. But - Using C# 6.0 Syntax:

public static class StringExtension
{
  public static string Truncate(this string s, int max) 
  { 
    return s?.Length > max ? s.Substring(0, max) : s ?? throw new ArgumentNullException(s); 
  }
}

It works like a charm:

"Truncate me".Truncate(8);
Result: "Truncate"

Solution 15 - C#

A similar variant with C# 6's Null propagation operator

public static string Truncate(this string value, int maxLength)
{
    return value?.Length <= maxLength ? value : value?.Substring(0, maxLength);
}

Please note, we are essentially checking if value is null twice here.

Solution 16 - C#

Taking @CaffGeek and simplifying it:

public static string Truncate(this string value, int maxLength)
    {
        return string.IsNullOrEmpty(value) ? value : value.Substring(0, Math.Min(value.Length, maxLength));
    }

Solution 17 - C#

Kndly note that truncating a string not merely means justing cutting a string at a specified length alone but have to take care not to split the word.

eg string : this is a test string.

I want to cut it at 11 . If we use any of the method given above the result will be

this is a te

This is not the thing we want

The method i am using may also not so perfect but it can handle most of the situation

public string CutString(string source, int length)
{
        if (source== null || source.Length < length)
        {
            return source;
        }
        int nextSpace = source.LastIndexOf(" ", length);
        return string.Format("{0}...", input.Substring(0, (nextSpace > 0) ? nextSpace : length).Trim());
} 

Solution 18 - C#

The simplest method in recent C# would be:

string Trunc(string s, int len) => s?.Length > len ? s.Substring(0, len) : s;

It returns truncated value for longer strings and original string for other cases - including null input - which is treated by ? unary operator.

Solution 19 - C#

Just in case there's not enough answers here, here's mine :)

public static string Truncate(this string str, 
                              int totalLength, 
                              string truncationIndicator = "")
{
	if (string.IsNullOrEmpty(str) || str.Length < totalLength) 
		return str;
		
	return str.Substring(0, totalLength - truncationIndicator.Length) 
           + truncationIndicator;
}

to use:

"I use it like this".Truncate(5,"~")

Solution 20 - C#

I know there are a ton of answers here already, but this is the one I have gone with, which handles both null strings and the situation where the length passed in is negative:

public static string Truncate(this string s, int length)
{
    return string.IsNullOrEmpty(s) || s.Length <= length ? s 
        : length <= 0 ? string.Empty 
        : s.Substring(0, length);
}

Solution 21 - C#

My two cents with example length of 30 :

  var truncatedInput = string.IsNullOrEmpty(input) ? 
      string.Empty : 
      input.Substring(0, Math.Min(input.Length, 30));

Solution 22 - C#

For the sake of (over)complexity I'll add my overloaded version which replaces the last 3 characters with an ellipsis in respect with the maxLength parameter.

public static string Truncate(this string value, int maxLength, bool replaceTruncatedCharWithEllipsis = false)
{
    if (replaceTruncatedCharWithEllipsis && maxLength <= 3)
        throw new ArgumentOutOfRangeException("maxLength",
            "maxLength should be greater than three when replacing with an ellipsis.");
    
    if (String.IsNullOrWhiteSpace(value)) 
        return String.Empty;

    if (replaceTruncatedCharWithEllipsis &&
        value.Length > maxLength)
    {
        return value.Substring(0, maxLength - 3) + "...";
    }

    return value.Substring(0, Math.Min(value.Length, maxLength)); 
}

Solution 23 - C#

There is nothing in .net for this that I am aware of - here is my version which adds "...":

public static string truncateString(string originalString, int length) {
  if (string.IsNullOrEmpty(originalString)) {
   return originalString;
  }
  if (originalString.Length > length) {
   return originalString.Substring(0, length) + "...";
  }
  else {
   return originalString;
  }
}

Solution 24 - C#

I prefer jpierson's answer, but none of the examples here that I can see are handling an invalid maxLength parameter, such as when maxLength < 0.

Choices would be either handle the error in a try/catch, clamp the maxLength parameter min to 0, or if maxLength is less than 0 return an empty string.

Not optimized code:

public string Truncate(this string value, int maximumLength)
{
    if (string.IsNullOrEmpty(value) == true) { return value; }
    if (maximumLen < 0) { return String.Empty; }
    if (value.Length > maximumLength) { return value.Substring(0, maximumLength); }
    return value;
}

Solution 25 - C#

Here is a vb.net solution, mark that the if (although ugly) statement improves performance because we do not need the substring statement when string is already smaller than maxlength... By making it an extention to string it is easy to use...

 <System.Runtime.CompilerServices.Extension()> _
    Public Function Truncate(String__1 As String, maxlength As Integer) As String
        If Not String.IsNullOrEmpty(String__1) AndAlso String__1.Length > maxlength Then
            Return String__1.Substring(0, maxlength)
        Else
            Return String__1
        End If
    End Function

Solution 26 - C#

I know there are a ton of answers already but my need was to keep the beginning and end of the string intact but shorten it to under the max length.

    public static string TruncateMiddle(string source)
    {
        if (String.IsNullOrWhiteSpace(source) || source.Length < 260) 
            return source;

        return string.Format("{0}...{1}", 
            source.Substring(0, 235),
            source.Substring(source.Length - 20));
    }

This is for creating SharePoint URLs that have a max length of 260 characters.

I didn't make length a parameter since it is a constant 260. I also didn't make the first substring length a parameter because I want it to break at a specific point. Finally, the second substring is the length of the source - 20 since I know the folder structure.

This could easily be adapted to your specific needs.

Solution 27 - C#

The popular library Humanizer has a Truncate method. To install with NuGet:

Install-Package Humanizer

Solution 28 - C#

Here's a C# 9 one-liner:

public static string Truncate(this string value, int maxLength) => value is null or "" || value.Length <= maxLength ? value : value[..maxLength];

Solution 29 - C#

TruncateString

public static string _TruncateString(string input, int charaterlimit)
{
    int characterLimit = charaterlimit;
    string output = input;

    // Check if the string is longer than the allowed amount
    // otherwise do nothing
    if (output.Length > characterLimit && characterLimit > 0)
    {
        // cut the string down to the maximum number of characters
        output = output.Substring(0, characterLimit);
        // Check if the character right after the truncate point was a space
        // if not, we are in the middle of a word and need to remove the rest of it
        if (input.Substring(output.Length, 1) != " ")
        {
            int LastSpace = output.LastIndexOf(" ");

            // if we found a space then, cut back to that space
            if (LastSpace != -1)
            {
                output = output.Substring(0, LastSpace);
            }
        }
        // Finally, add the "..."
        output += "...";
    }
    return output;
}

Solution 30 - C#

As an addition to the possibilities discussed above I'd like to share my solution. It's an extension method that allows null (returns string.Empty) also there is a second .Truncate() for using it with an ellipsis. Beware, it's not performance optimized.

public static string Truncate(this string value, int maxLength) =>
    (value ?? string.Empty).Substring(0, (value?.Length ?? 0) <= (maxLength < 0 ? 0 : maxLength) ? (value?.Length ?? 0) : (maxLength < 0 ? 0 : maxLength));
public static string Truncate(this string value, int maxLength, string ellipsis) =>
    string.Concat(value.Truncate(maxLength - (((value?.Length ?? 0) > maxLength ? ellipsis : null)?.Length ?? 0)), ((value?.Length ?? 0) > maxLength ? ellipsis : null)).Truncate(maxLength);

Solution 31 - C#

You can create a Truncate extension method that compares the max length relative to the string length and calls Substring if needed.

If you want null handling behavior that parallels that of Substring, don't include a null check. That way, just as str.Substring(0, 10) throws a NullReferenceException if str is null, so will str.Truncate(10).

public static class StringExtensions
{
    public static string Truncate(this string value, int maxLength) =>
        value.Length <= maxLength ? value : value.Substring(0, maxLength); 
}

Solution 32 - C#

Based on this, and this, here are two versions that will work for negative values of 'up to' values as well. This first one doesn't allow negative values silently by capping at 0:

public static string Truncate(this string value, int maxLength)
{
    return string.IsNullOrEmpty(value) ?
        value :
        value.Substring(0, Math.Max(0, Math.Min(value.Length, maxLength)));
}

This one goes in circle:

private static int Mod(this int a, int n) => (((a %= n) < 0) ? n : 0) + a;

public static string Truncate(this string value, int maxLength)
{
    return string.IsNullOrEmpty(value) ?
        value :
        value.Substring(0, maxLength.Mod(value.Length));
}

Solution 33 - C#

All the other answers do not take into account for Span's which is more performant than the Substring of the string type in .NET

If you are not already aware there is a version of System.Memory (which provides Span, ReadOnlySpan, Memory, and ReadOnlyMemory's for cases like this:

As such simple implementation for this could be as follows:

public static string Truncate(this string value, int maxLength)
{
    if (!string.IsNullOrEmpty(value) && value.Length > maxLength)
    {
        return value.AsSpan(0, maxLength).ToString(); /* Note the use of AsSpan instead of Substring. */
    }

    return value;
}

The method could theoretically be made to return Span<char> instead to avoid allocating a new string using the ToString() member of Span<T>.

The BCL itself internally uses Span's, ReadOnlySpan's, Memory's, and ReadOnlyMemory's where possible to avoid issues and to help optimize the code, especially when you compile arrays that are known at compile time and so using an property that returns that new'd up array as an ReadOnlySpan<byte> actually optimizes the code at runtime as then the JIT would not call memcpy on the data and instead uses it since it just returns a Span and as such is a window to the data that is already allocated ahead of time resulting in:

  1. less allocations.
  2. Less time spent allocating.
  3. makes the code overall faster to use.

Solution 34 - C#

public static string Truncate( this string value, int maxLength )
    {
        if (string.IsNullOrEmpty(value)) { return value; }

        return new string(value.Take(maxLength).ToArray());// use LINQ and be happy
    }

Solution 35 - C#

Truncate String

public static string TruncateText(string strText, int intLength)
{
    if (!(string.IsNullOrEmpty(strText)))
    {                                
        // split the text.
        var words = strText.Split(' ');

        // calculate the number of words
        // based on the provided characters length 
        // use an average of 7.6 chars per word.
        int wordLength = Convert.ToInt32(Math.Ceiling(intLength / 7.6));

        // if the text is shorter than the length,
        // display the text without changing it.
        if (words.Length <= wordLength)
            return strText.Trim();                

        // put together a shorter text
        // based on the number of words
        return string.Join(" ", words.Take(wordLength)) + " ...".Trim();
    }
        else
        {
            return "";
        }            
    }

Solution 36 - C#

This is the code I usually use:

string getSubString(string value, int index, int length)
        {
            if (string.IsNullOrEmpty(value) || value.Length <= length)
            {
                return value;
            }
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            for (int i = index; i < length; i++)
            {
                sb.AppendLine(value[i].ToString());
            }
            return sb.ToString();
        }

Solution 37 - C#

I'd Recommend using the substring method for the same effective functionality.

    // Gets first n characters.
    string subString = inputString.Substring(0, n);

This has the benefit of letting you splice your string from either side or even somewhere in the middle without writing additional methods. Hope that helps :)

For additional reference: https://www.dotnetperls.com/substring

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSteve GuidiView Question on Stackoverflow
Solution 1 - C#LBushkinView Answer on Stackoverflow
Solution 2 - C#CaffGeekView Answer on Stackoverflow
Solution 3 - C#jpiersonView Answer on Stackoverflow
Solution 4 - C#drzausView Answer on Stackoverflow
Solution 5 - C#Dylan NicholsonView Answer on Stackoverflow
Solution 6 - C#tamesView Answer on Stackoverflow
Solution 7 - C#Marek MalczewskiView Answer on Stackoverflow
Solution 8 - C#SeanMCView Answer on Stackoverflow
Solution 9 - C#JoeView Answer on Stackoverflow
Solution 10 - C#DarrenView Answer on Stackoverflow
Solution 11 - C#SunsetQuestView Answer on Stackoverflow
Solution 12 - C#nologoView Answer on Stackoverflow
Solution 13 - C#SriView Answer on Stackoverflow
Solution 14 - C#Retracted for PrivacyView Answer on Stackoverflow
Solution 15 - C#Jamie ReesView Answer on Stackoverflow
Solution 16 - C#Edwin BeltranView Answer on Stackoverflow
Solution 17 - C#Sen K. MathewView Answer on Stackoverflow
Solution 18 - C#BohdanView Answer on Stackoverflow
Solution 19 - C#K. R. View Answer on Stackoverflow
Solution 20 - C#Ed BView Answer on Stackoverflow
Solution 21 - C#Ognyan DimitrovView Answer on Stackoverflow
Solution 22 - C#SoftDevView Answer on Stackoverflow
Solution 23 - C#RayView Answer on Stackoverflow
Solution 24 - C#deegeeView Answer on Stackoverflow
Solution 25 - C#Jeroen BomView Answer on Stackoverflow
Solution 26 - C#Paul HaanView Answer on Stackoverflow
Solution 27 - C#Edward BreyView Answer on Stackoverflow
Solution 28 - C#chillitomView Answer on Stackoverflow
Solution 29 - C#SudView Answer on Stackoverflow
Solution 30 - C#Raymond OsterbrinkView Answer on Stackoverflow
Solution 31 - C#Edward BreyView Answer on Stackoverflow
Solution 32 - C#NaeView Answer on Stackoverflow
Solution 33 - C#PSXGamerPro1View Answer on Stackoverflow
Solution 34 - C#TapiocaComView Answer on Stackoverflow
Solution 35 - C#V.T.View Answer on Stackoverflow
Solution 36 - C#user3390116View Answer on Stackoverflow
Solution 37 - C#SethGoodluckView Answer on Stackoverflow