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


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

The best solution than even is the second one!


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)

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