Wednesday 10 June 2015

Add the ... after string 50 character

Add the ... after string lengh

  public string strStringTruncates(string s, int length)
        {
            if (String.IsNullOrEmpty(s))
                throw new ArgumentNullException(s);
            var words = s.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            if (words[0].Length > length)
                return words[0];
            var sb = new StringBuilder();

            foreach (var word in words)
            {
                if ((sb + word).Length > length)
                    return string.Format("{0}...", sb.ToString().TrimEnd(' '));
                sb.Append(word + " ");
            }
            return string.Format("{0}...", sb.ToString().TrimEnd(' '));

        }


Call the method:-
lblTechnologyUsed.Text = strStringTruncates((e.Row.DataItem as DataRowView).Row["Technology_x0020_Used"].ToString().Trim(), 50);



No comments:

Post a Comment