Created
August 26, 2010 20:25
-
-
Save Restuta/552176 to your computer and use it in GitHub Desktop.
Various useful System.String extensions
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.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
namespace AutoAssemblyVersion | |
{ | |
/// <summary> | |
/// Contains string extensions. | |
/// </summary> | |
public static class StringExtensions | |
{ | |
/// <summary> | |
/// Determines whether the beginning of this instance matches one of the specified chars. | |
/// </summary> | |
/// <param name="source">The source.</param> | |
/// <param name="chars">The chars.</param> | |
/// <returns></returns> | |
public static bool StartsWithAny(this string source, IEnumerable<char> chars) | |
{ | |
if (chars == null) | |
{ | |
throw new ArgumentNullException("chars"); | |
} | |
return chars.Any(character => source.StartsWith(character.ToString())); | |
} | |
/// <summary> | |
/// Removes provided string from source string. | |
/// </summary> | |
/// <param name="source">The source.</param> | |
/// <param name="stringToRemove">The string to remove.</param> | |
/// <returns></returns> | |
public static string Remove(this string source, string stringToRemove) | |
{ | |
if (stringToRemove == null) | |
{ | |
throw new ArgumentNullException("stringToRemove"); | |
} | |
if (!source.Contains(stringToRemove)) | |
{ | |
throw new ArgumentException("Source string doesn't contain provided for removal string."); | |
} | |
return source.Replace(stringToRemove, string.Empty); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment