Cleaning up some string extension methods

--HG--
branch : dev
This commit is contained in:
Nathan Heskew
2010-12-02 07:30:33 -08:00
parent fd6dc8b92b
commit bf005f20a2

View File

@@ -6,8 +6,8 @@ namespace Orchard.Utility.Extensions {
public static class StringExtensions {
private static readonly Regex humps = new Regex("(?:^[a-zA-Z][^A-Z]*|[A-Z][^A-Z]*)");
public static string CamelFriendly(this string camel) {
if (camel == null)
return null;
if (string.IsNullOrWhiteSpace(camel))
return "";
var matches = humps.Matches(camel).OfType<Match>().Select(m => m.Value);
return matches.Any()
@@ -20,17 +20,16 @@ namespace Orchard.Utility.Extensions {
}
public static string Ellipsize(this string text, int characterCount, string ellipsis) {
var cleanTailRegex = new Regex(@"\s+\S*$");
if (string.IsNullOrWhiteSpace(text) || characterCount < 0 || text.Length <= characterCount)
return "";
if (string.IsNullOrEmpty(text) || characterCount < 0 || text.Length <= characterCount)
return text;
return cleanTailRegex.Replace(text.Substring(0, characterCount + 1), "") + ellipsis;
return Regex.Replace(text.Substring(0, characterCount + 1), @"\s+\S*$", "") + ellipsis;
}
public static string HtmlClassify(this string text) {
if (string.IsNullOrWhiteSpace(text))
return text;
return "";
var friendlier = text.CamelFriendly();
return Regex.Replace(friendlier, @"[^a-zA-Z]+", m => m.Index == 0 ? "" : "-").ToLowerInvariant();
}
@@ -42,10 +41,7 @@ namespace Orchard.Utility.Extensions {
}
public static string RemoveTags(this string html) {
var tagRegex = new Regex("<[^<>]*>", RegexOptions.Singleline);
var text = tagRegex.Replace(html, "");
return text;
return Regex.Replace(html, "<[^<>]*>", "", RegexOptions.Singleline);
}
}
}