Skip to content

Instantly share code, notes, and snippets.

@hardye
Created February 4, 2014 09:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hardye/8800908 to your computer and use it in GitHub Desktop.
Save hardye/8800908 to your computer and use it in GitHub Desktop.
Some useful extension methods for strings.
/// <summary>
/// Extension methods for strings.
/// </summary>
public static class StringExtensions
{
/// <summary>
/// Truncates a string using the specified options.
/// </summary>
/// <param name="val">The value to truncate.</param>
/// <param name="maxLength">Maximum length of the output string.</param>
/// <param name="options">The options.</param>
/// <remarks>Based on http://www.codeproject.com/Articles/47930/String-Truncation-Function-for-C</remarks>
public static string Truncate(this string val, int maxLength, TruncateOptions options)
{
if (val == null)
{
return "";
}
if (val.Length <= maxLength)
{
return val;
}
bool includeEllipsis = (options & TruncateOptions.IncludeEllipsis) == TruncateOptions.IncludeEllipsis;
bool finishWord = (options & TruncateOptions.FinishWord) == TruncateOptions.FinishWord;
bool allowLastWordOverflow = (options & TruncateOptions.AllowLastWordToGoOverMaxLength) == TruncateOptions.AllowLastWordToGoOverMaxLength;
string retValue = val;
if (includeEllipsis)
{
maxLength -= 1;
}
int lastSpaceIndex = retValue.LastIndexOf(" ", maxLength, StringComparison.CurrentCultureIgnoreCase);
if (!finishWord)
{
retValue = retValue.Remove(maxLength);
}
else if (allowLastWordOverflow)
{
int spaceIndex = retValue.IndexOf(" ", maxLength, StringComparison.CurrentCultureIgnoreCase);
if (spaceIndex != -1)
{
retValue = retValue.Remove(spaceIndex);
}
}
else if (lastSpaceIndex > -1)
{
retValue = retValue.Remove(lastSpaceIndex);
}
if (includeEllipsis && retValue.Length < val.Length)
{
retValue += " ...";
}
return retValue;
}
public static string StripHtml(this string val)
{
//Remove any HTML
Regex htmlRegex = new Regex("</?[a-z][a-z0-9]*[^<>]*>|<!--.*?-->", RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.Compiled);
val = htmlRegex.Replace(val, String.Empty);
//Remove any HTML non-breaking spaces or other white space from the beginning of the string
#region Regex Definition
// ^(?:\s|&nbsp;)+
//
// Options: case insensitive; ^ and $ match at line breaks
//
// Assert position at the beginning of a line (at beginning of the string or after a line break character) «^»
// Match the regular expression below «(?:\s|&nbsp;)+»
// Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
// Match either the regular expression below (attempting the next alternative only if this one fails) «\s»
// Match a single character that is a “whitespace character” (spaces, tabs, and line breaks) «\s»
// Or match regular expression number 2 below (the entire group fails if this one fails to match) «&nbsp;»
// Match the characters “&nbsp;” literally «&nbsp;»
#endregion
Regex wsRegex = new Regex(@"^(?:\s|&nbsp;)+", RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.Compiled);
return wsRegex.Replace(val, String.Empty);
}
}
[Flags]
public enum TruncateOptions
{
None = 0x0,
FinishWord = 0x1,
AllowLastWordToGoOverMaxLength = 0x2,
IncludeEllipsis = 0x4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment