Shrink string with ... - CSharp System

CSharp examples for System:String Shorten

Description

Shrink string with ...

Demo Code


using System.Text.RegularExpressions;
using System.Linq;
using System.Collections.Generic;
using System;/*w  w w.j a va 2s  .  c  om*/

public class Main{
        public static string Take(this string str, int count, bool ellipsis = false)
        {
            var lengthToTake = Math.Min(count, str.Length);
            var cutDownString = str.Substring(0, lengthToTake);

            if (ellipsis && lengthToTake < str.Length)
                cutDownString += "...";

            return cutDownString;
        }
}

Related Tutorials