Created
February 4, 2014 09:58
-
-
Save hardye/8800908 to your computer and use it in GitHub Desktop.
Some useful extension methods for strings.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <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| )+ | |
// | |
// 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| )+» | |
// 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) « » | |
// Match the characters “ ” literally « » | |
#endregion | |
Regex wsRegex = new Regex(@"^(?:\s| )+", 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