Skip to content

Instantly share code, notes, and snippets.

@r15ch13
Forked from scottoffen/StringExtensions.cs
Last active August 29, 2015 14:15
Show Gist options
  • Save r15ch13/284e6ba207942526e576 to your computer and use it in GitHub Desktop.
Save r15ch13/284e6ba207942526e576 to your computer and use it in GitHub Desktop.
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