Last active
August 29, 2015 14:00
-
-
Save scottoffen/9c62a39a2b1787af17e6 to your computer and use it in GitHub Desktop.
My favorite C# string extension methods!
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
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