Skip to content

Instantly share code, notes, and snippets.

@angelobelchior
Created March 28, 2014 19:03
Show Gist options
  • Save angelobelchior/9840498 to your computer and use it in GitHub Desktop.
Save angelobelchior/9840498 to your computer and use it in GitHub Desktop.
using System.Linq;
using System.Text;
namespace System
{
public static class StringExtentions
{
public static string RemoveNonNumeric(this string self)
{
Throw.IfIsNullOrEmpty(self);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < self.Length; i++)
if (Char.IsNumber(self[i]))
sb.Append(self[i]);
return sb.ToString();
}
public static string RemoveNumeric(this string self)
{
Throw.IfIsNullOrEmpty(self);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < self.Length; i++)
if (!Char.IsNumber(self[i]))
sb.Append(self[i]);
return sb.ToString();
}
public static string SentenceCase(this string self)
{
Throw.IfIsNullOrEmpty(self);
if (self.Length < 1)
return self;
string sentence = self.ToLower();
return sentence[0].ToString().ToUpper() +
sentence.Substring(1);
}
public static string TitleCase(this string self)
{
Throw.IfIsNullOrEmpty(self);
if (self.Length == 0) return string.Empty;
string[] tokens = self.Split(' ');
StringBuilder sb = new StringBuilder(self.Length);
foreach (string s in tokens)
{
sb.Append(s[0].ToString().ToUpper());
sb.Append(s.Substring(1).ToLower());
sb.Append(" ");
}
return sb.ToString().Trim();
}
public static string Truncate(this string self, int length, string suffix = "")
{
Throw.IfIsNullOrEmpty(self);
Throw.IfLessThanOrEqZero(length);
if (self.Length <= length) return self;
var fragment = self.Substring(0, length);
return string.Format("{0}{1}", fragment, suffix);
}
public static string Right(this string self, int length)
{
Throw.IfIsNullOrEmpty(self);
Throw.IfLessThanZero(length);
return self.Length > length ? self.Substring(self.Length - length) : self;
}
public static string Left(this string self, int length)
{
Throw.IfIsNullOrEmpty(self);
Throw.IfLessThanZero(length);
return self.Length > length ? self.Substring(0, length) : self;
}
public static bool In(this string self, params string[] items)
{
Throw.IfIsNullOrEmpty(self);
Throw.IfIsNull(items);
Throw.IfEqZero(items.Length);
return items.Contains(self);
}
public static string RemoveLastChar(this string self)
{
Throw.IfIsNullOrEmpty(self);
return self.Left(self.Length - 1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment