Skip to content

Instantly share code, notes, and snippets.

@scottoffen
Last active August 29, 2015 14:00
Show Gist options
  • Save scottoffen/9c62a39a2b1787af17e6 to your computer and use it in GitHub Desktop.
Save scottoffen/9c62a39a2b1787af17e6 to your computer and use it in GitHub Desktop.
My favorite C# string extension methods!
using System;
using System.Text.RegularExpressions;
namespace YourNamespace
{
public static class StringExtensions
{
public static string Capitalize(this String s)
{
if (!String.IsNullOrEmpty(s))
{
return char.ToUpper(s[0]) + s.Substring(1).ToLower();
}
return s;
}
public static GroupCollection GrabAll(this String s, string pattern, bool ignoreCase = true)
{
Match match = (ignoreCase) ? Regex.Match(s, pattern, RegexOptions.IgnoreCase) : Regex.Match(s, pattern);
return match.Groups;
}
public static string GrabFirst(this String s, string pattern, bool ignoreCase = true)
{
Match match = (ignoreCase) ? Regex.Match(s, pattern, RegexOptions.IgnoreCase) : Regex.Match(s, pattern);
if (match.Success)
{
return match.Groups[1].Value;
}
return null;
}
public static bool Matches(this String s, string pattern, bool ignoreCase = true)
{
if (ignoreCase)
{
return ((Regex.IsMatch(s, pattern, RegexOptions.IgnoreCase))) ? true : false;
}
else
{
return (Regex.IsMatch(s, pattern)) ? true : false;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment