Crops a given text
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.ComponentModel;
namespace NearForums
{
public static class Utils
{
/// <summary>
/// Crops a given text
/// </summary>
/// <param name="value">the text to summarize</param>
/// <param name="maxChars">maximum chars allowed</param>
/// <param name="appendIfCropped">text to be appended if the text is cropped. For example: ...</param>
/// <returns></returns>
public static string Summarize(string value, int maxChars, string appendIfCropped)
{
if (value == null)
{
return null;
}
if (value.Length <= maxChars)
{
return value;
}
value = value.Substring(0, maxChars);
Match match = Regex.Match(value, @"^.*\b(?=[ \.])", RegexOptions.Singleline);
if (match.Success)
{
value = match.Value;
}
if (appendIfCropped != null)
{
value += appendIfCropped;
}
return value;
}
}
}
Related examples in the same category