Advertisement
Advertisement

Very often I found this problem and now I got a solution!

I’ve compared some methods and I was decided which one. But I verified time (if you want to know how, read this post) of them and the final solutions are:

static string TruncateLongString(string str, int maxLength)
{
    return str.Substring(0, Math.Min(str.Length, maxLength));
}

or

Advertisement
string TruncateString(string str, int maxLength)
{
    return new string(str.Take(maxLength).ToArray());
}

The best solution than even is the second one!

TruncateString

Advertisement

By Enrico

My greatest passion is technology. I am interested in multiple fields and I have a lot of experience in software design and development. I started professional development when I was 6 years. Today I am a strong full-stack .NET developer (C#, Xamarin, Azure)

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.