552

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?

6
  • 31
    For the record, strings are immutable you can't truncate them you can only return a truncated copy of them. Nitpicky, I know. Commented May 5, 2010 at 20:55
  • 3
    @John Weldon: That's probably why the member function doesn't exist -- it doesn't follow the semantics of the datatype. On a side note, StringBuilder lets you truncate by shorterning the length, but you still need to perform the length check to avoid widening the string. Commented May 5, 2010 at 20:59
  • 1
    Whichever solution you pick, be sure to add a check for a null string before calling Substring or accessing the Length property. Commented May 5, 2010 at 20:59
  • 3
    @SteveGuidi - If that were the case, then there wouldn't be functions like Trim or Replace, which face similar semantic problems Commented Aug 3, 2016 at 23:20
  • 3
    @JohnWeldon More nitpicky than Microsoft themselves consistently are, as it happens - they're happy to document, for instance, .Trim() in a manner that makes it misleadingly sound like it mutates the string: "Removes all leading and trailing white-space characters from the current String object." Commented Jan 13, 2018 at 21:41

41 Answers 41

844

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
Sign up to request clarification or add additional context in comments.

18 Comments

Great Solution, but remembered this only works in NET 3.5 and Up. Don't try it in NET2.0.
As long as you're in VS 2008, and presumably VS 2010, you could still do this even if targeting .Net 2.0. danielmoth.com/Blog/…
@Bernard, this is supposed to fail if maxLength is negative. Any other behavior would be unexpected.
You can call extension methods on null values.
It could be useful to add an optional third parameter, string ellipsis = "", which you would use like "myLongUsername".Truncate(9, "...") which would return "myLong...". The length of the ellipses would have to be accounted for where you truncate the string, so the code would basically change from value.Substring(0, maxLength) to value.Substring(0, maxLength - ellipsis.Length) + ellipses.
|
180

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));
    }
}

6 Comments

Clever! And the following expression is optimized to return a reference to the original string: value.Substring(0, value.Length).
Unfortunately it's not optimized for cases where value.Length is less than MaxLength which may be a common case in some data. Also the Length property on string should be capitalized.
This will fail when maxLength is a negative value.
@Bernard, so will a lot of things in the framework...but if I check for it...I either have to default maxLength to 0 or value.Length; or I need to throw an ArgumentOutOfRangeException...which makes more sense in this case, and is already thrown by Substring anyhow.
A bit shorter: return string.IsNullOrEmpty(value) ? value : value.Substring(0, Math.Min(value.Length, maxLength));
|
58

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]

3 Comments

Thanks for all the useful benchmarks! ... and Linkpad rocks!
Hmm, I don't agree with this bench. 1) The input string val is a constant. 2) The truncation length limit is constant for all 10k iterations. 3) It seems the output is immediately discarded. All these factors can cause the compiler to optimize things away. I did my own bench and found what you call "smart-trunc" to on average be significantly (20%) faster than "truncate", and that was with 10M iterations in Release/production mode (I also found significant difference between running in Debug mode and Release mode)
@JHBonarius i figured that the YMMV caveat for performance tests should be somewhat obvious and i just wanted to get that ball rolling ;) and even if you're absolutely right, don't forget we're talking about differences less than a thousandth of a millisecond...
55

In .NET 4.0 you can use the Take method:

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

Not tested for efficiency!

1 Comment

Even though it's slower, this one is great as a oneliner because it references the original string only once. Very helpful to truncate a resulting string of some statement like string.Concat(myString.Replace("shorty","longlonglonglong").Take(maxLength));
50

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.

5 Comments

This will fail when maxLength is a negative value.
@Bernard - I would recommend not passing a negative value for the maxLength argument as it is an unexpected value. The Substring method takes the same approach so there is no reason to improve on the exception it throws.
I don't think the IsNullOrEmpty check is necessary? (1) If value is null, there should be no way to call this extension method on it. (2) If value is the empty string, the value.Length > maxLength check will fail.
@JonSchneider, the IsNullOrEmpty is required because this is an extension method. If you have a variable of type string that has been assigned a null, the compiler does not insert a null check before calling this method. Technically, this is still a static method of the static class. So: stringVar.Truncate(2) Compiles as: StringExt.Truncate(stringVar, 2);
The lack of an IsNullOrEmpty and the subsequent NullReferenceException is a feature, not a bug. Just as str.Substring(0,10) throws if str is null, it makes sense for str.Truncate(10) to do likewise.
41

Another solution:

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

3 Comments

Very elegant solution +1
Nice, but this won't work on a string that is null.
This looks too similar to this other answer: stackoverflow.com/a/2776720/475876
32

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());

3 Comments

why is this not the accepted answer? What's most straight forward, writing your own Extension method that you need to maintain/document, or using something BUILT IN like .Take
@mmcrae Linq might be more straight forward, but it's also a lot slower. My benchmark says ~400ms for Linq and only ~24ms for Substring for 1 million iterations.
This solution shouldn't be ever used. As said in two above comments, there always is memory allocation, even when existing string is not bigger than the max length. Also it's very slow.
23

I did mine in one line sort of like this

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

1 Comment

Quick, simple and fast. This is what I needed. Thanks!
21

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

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

6 Comments

This is easily the best way now. One line, no extension method, bueno! Highly suggest this be switched to the accepted answer.
It does same as here: stackoverflow.com/a/54491582/1521008 under the hood: value = value.Substring(0, Math.Min(30, value.Length));
@SerjG - Your right! I think at the time I did not realize that it was just some nice syntax sugar that ends up being like almost every other answer on this thread. =) It's cleaner at least. (well arguable). I feel like C# should have a string.left function for this... like value.left(30). Other languages have this, but I guess it might pollute the namespace.
@SunsetQuest I like it more too. But I expected to see the Span under the hood :( Maybe Span doesn't give any profit because of the ToString() in the end and same allocation?
@SerjG - I think you're right; it needs to create a string on the heap regardless. I tried using a ReadOnlySpan<char> value2 = value[..Math.Min(30, value.Length)], but it still calls value.Substring first and then casts it to a ReadOnlySpan. It's surprising it does that, as it doesn't seem especially efficient.
|
17

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.

5 Comments

"The .NET Framework has an API" you are contradicting yourself. That's a VB.NET API
@CamiloTerevinto - it's an API that is shipped with the .NET Framework, and can be called from any managed language.
The VB DLL has a lot of good stuff in it. Why are so many c# devs against it?
No .NET Core support currently, unfortunately. Indeed, the entire Microsoft.VisualBasic.Strings modules in .NET Core is pretty empty.
While I agree with Joe comment, still I don't feel right calling something specific for VB from other languages. If there are so much good stuff in "VB DLL", why not put it in some shared place? Who knows what Microsoft will do with these stuff tomorrow? Stop support or something..
16

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.

Comments

8

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];

4 Comments

This is a nice one. The only observation I have is that it's better to use string.Empty than "" for an empty string, for performance reasons...
@EdB this is a pattern match so has to be a string literal. That said, in normal expressions both string.Empty and "" resolve to the same interned string at run time.
turns out that you are right (post .Net 2.0), I guess you pick up habits which have good reasons for them, and those reasons go away, but the habits remain!
Empty string check is not necessary considering the following maxLength check
7

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...

1 Comment

@SarjanWebDev That special character shows up as "." in cmd.exe
7

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.

Comments

6

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"

Comments

6

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.

3 Comments

You generate twice as many string objects and it could throw a NullReferenceException from the PadRight call if value is null which is inappropriate, it should be an ArgumentNullException.
@Jeremy I don't understand " it could throw a NullReferenceException from the PadRight call if value is null"; have I not mentioned "//check string.IsNullOrEmpty(value) and act on it."
That function does not produce the string expected by the original question, but I'm not downvoting it because it can can handy for someone else.
6

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.

Comments

5

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));
    }

Comments

5

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.

Comments

5

My two cents with example length of 30 :

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

Comments

4

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());
} 

1 Comment

That is not what the original question asked but I'm not downvoting your answer because it can come handy for someone else.
4

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);
}

Comments

3

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,"~")

Comments

3

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

Install-Package Humanizer

Comments

3

With C# 8 range feature and considering all kinds of inputs:

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

Comments

2

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)); 
}

Comments

1

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;
  }
}

1 Comment

Your version will give strings that are 3 characters longer than the requested length, in case they are truncated. Besides, the triple dots are really just meaningful in representation, I would not store it in a database like that which is the use case that the OP gave.
1

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;
}

1 Comment

Note, that in my implementation I chose not to handle the case where maximumLength is less than 0 because I figured the only thing I would do is throw an ArgumentOutOfRangeExcpetion which essentially what string.Substring() does for me.
1

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

2 Comments

In VB.net you can replace "Not String.IsNullOrEmpty(String__1)" with "String__1 <> Nothing". It's a little bit shorter. The default value for strings is an empty string. Using "<> Nothing" checks both null and the empty string cases. Test it out with: Truncate("", 50) and Truncate(Nothing, 50)
In VB you can do Left(string, maxlength)
1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.