public static class StringExtensions
{
#region - Substring Methods -
/// <summary>
/// Gets the substring after the first occurrence of a separator. The separator is not returned.
/// </summary>
/// <remarks>
/// A null string input will return null. An empty ("") string input will return the empty string.
/// A null separator will return the empty string if the input string is not null.
/// </remarks>
/// <example>
/// StringExtensions.SubstringAfter(null, "*") = null;
/// StringExtensions.SubstringAfter("", "*") = "";
/// StringExtensions.SubstringAfter("*", null) = "";
/// StringExtensions.SubstringAfter("abc", "a") = "bc";
/// StringExtensions.SubstringAfter("abcba", "b") = "cba";
/// StringExtensions.SubstringAfter("abc", "c") = "";
/// StringExtensions.SubstringAfter("abc", "d") = "";
/// StringExtensions.SubstringAfter("abc", "") = "abc";
/// </example>
/// <param name="str">the String to get a substring from, may be null</param>
/// <param name="separator">the String to search for, may be null </param>
/// <returns>the substring after the first occurrence of the separator, null if null String input</returns>
public static string SubstringAfter(this string str, string separator)
{
if (string.IsNullOrEmpty(str))
return str;
if (separator == null)
return string.Empty;
CompareInfo compareInfo = CultureInfo.InvariantCulture.CompareInfo;
int index = compareInfo.IndexOf(str, separator, CompareOptions.Ordinal);
if (index < 0)
{
//No such substring
return string.Empty;
}
return str.Substring(index + separator.Length);
}
/// <summary>
/// Gets the substring after the last occurrence of a separator. The separator is not returned.
/// </summary>
/// <remarks>
/// A null string input will return null. An empty ("") string input will return the empty string.
/// An empty or null separator will return the empty string if the input string is not null.
/// </remarks>
/// <example>
/// StringExtensions.SubstringAfterLast(null, "*") = null;
/// StringExtensions.SubstringAfterLast("", "*") = "";
/// StringExtensions.SubstringAfterLast("*", "") = "";
/// StringExtensions.SubstringAfterLast("*", null) = "";
/// StringExtensions.SubstringAfterLast("abc", "a") = "bc";
/// StringExtensions.SubstringAfterLast("abcba", "b") = "a";
/// StringExtensions.SubstringAfterLast("abc", "c") = "";
/// StringExtensions.SubstringAfterLast("a", "a") = "";
/// StringExtensions.SubstringAfterLast("a", "z") = "";
/// </example>
/// <param name="str">the String to get a substring from, may be null</param>
/// <param name="separator">the String to search for, may be null </param>
/// <returns>the substring after the last occurrence of the separator, null if null String input</returns>
public static string SubstringAfterLast(this string str, string separator)
{
if (string.IsNullOrEmpty(str))
return str;
if (string.IsNullOrEmpty(separator))
return string.Empty;
CompareInfo compareInfo = CultureInfo.InvariantCulture.CompareInfo;
int index = compareInfo.LastIndexOf(str, separator, CompareOptions.Ordinal);
if (index < 0)
{
//No such substring
return string.Empty;
}
return str.Substring(index + separator.Length);
}
/// <summary>
/// Gets the substring before the first occurrence of a separator. The separator is not returned.
/// </summary>
/// <remarks>
/// A null string input will return null. An empty ("") string input will return the empty string.
/// A null separator will return the input string.
/// </remarks>
/// <example>
/// StringExtensions.SubstringBefore(null, "*") = null;
/// StringExtensions.SubstringBefore("", "*") = "";
/// StringExtensions.SubstringBefore("abc", "a") , "";
/// StringExtensions.SubstringBefore("abcba", "b") , "a";
/// StringExtensions.SubstringBefore("abc", "c") , "ab";
/// StringExtensions.SubstringBefore("abc", "d") , "abc";
/// StringExtensions.SubstringBefore("abc", "") , "";
/// StringExtensions.SubstringBefore("abc", null) , "abc";
/// </example>
/// <param name="str">the String to get a substring from, may be null</param>
/// <param name="separator">the String to search for, may be null </param>
/// <returns>the substring before the first occurrence of the separator, null if null String input</returns>
public static string SubstringBefore(this string str, string separator)
{
if (string.IsNullOrEmpty(str) || separator == null)
return str;
if (separator == string.Empty)
return string.Empty;
CompareInfo compareInfo = CultureInfo.InvariantCulture.CompareInfo;
int index = compareInfo.IndexOf(str, separator, CompareOptions.Ordinal);
if (index < 0)
{
//No such substring
return str;
}
return str.Substring(0, index);
}
/// <summary>
/// Gets the substring before the last occurrence of a separator. The separator is not returned.
/// </summary>
/// <remarks>
/// A null string input will return null. An empty ("") string input will return the empty string.
/// An empty or null separator will return the input string.
/// </remarks>
/// <example>
/// StringExtensions.SubstringBeforeLast(null, "*") = null;
/// StringExtensions.SubstringBeforeLast("", "*") = "";
/// StringExtensions.SubstringBeforeLast("abcba", "b") = "abc";
/// StringExtensions.SubstringBeforeLast("abc", "c") = "ab";
/// StringExtensions.SubstringBeforeLast("a", "a") = "";
/// StringExtensions.SubstringBeforeLast("a", "z") = "a";
/// StringExtensions.SubstringBeforeLast("a", null) = "a";
/// StringExtensions.SubstringBeforeLast("a", "") = "a";
/// </example>
/// <param name="str">the String to get a substring from, may be null</param>
/// <param name="separator">the String to search for, may be null</param>
/// <returns>the substring before the last occurrence of the separator, null if null String input</returns>
public static string SubstringBeforeLast(this string str, string separator)
{
if (string.IsNullOrEmpty(str) || string.IsNullOrEmpty(separator))
return str;
CompareInfo compareInfo = CultureInfo.InvariantCulture.CompareInfo;
int index = compareInfo.LastIndexOf(str, separator, CompareOptions.Ordinal);
if (index < 0)
{
//No such substring
return str;
}
return str.Substring(0, index);
}
/// <summary>
/// Gets the String that is nested in between two instances of the same String.
/// </summary>
/// <remarks>A null input String returns null. A null tag returns null.</remarks>
/// <example>
/// StringExtensions.SubstringBetween(null, "*") = null;
/// StringExtensions.SubstringBetween("", "") = "";
/// StringExtensions.SubstringBetween("", "tag") = null;
/// StringExtensions.SubstringBetween("tagabctag", null) = null;
/// StringExtensions.SubstringBetween("tagabctag", "") = "";
/// StringExtensions.SubstringBetween("tagabctag", "tag") = "abc";
/// </example>
/// <param name="str">the String containing the substring, may be null</param>
/// <param name="tag">the String before and after the substring, may be null </param>
/// <returns>the substring, null if no match</returns>
public static string SubstringBetween(this string str, string tag)
{
return SubstringBetween(str, tag, tag);
}
/// <summary>
/// Gets the String that is nested in between two Strings. Only the first match is returned.
/// </summary>
/// <remarks>
/// A null input String returns null. A null open/close returns null (no match).
/// An empty ("") open and close returns an empty string.
/// </remarks>
/// <example>
/// StringExtensions.SubstringBetween("wx[b]yz", "[", "]") = "b";
/// StringExtensions.SubstringBetween(null, "*", "*") = null;
/// StringExtensions.SubstringBetween("*", null, "*") = null;
/// StringExtensions.SubstringBetween("*", "*", null) = null;
/// StringExtensions.SubstringBetween("", "", "") = "";
/// StringExtensions.SubstringBetween("", "", "]") = null;
/// StringExtensions.SubstringBetween("", "[", "]") = null;
/// StringExtensions.SubstringBetween("yabcz", "", "") = "";
/// StringExtensions.SubstringBetween("yabcz", "y", "z") = "abc";
/// StringExtensions.SubstringBetween("yabczyabcz", "y", "z") = "abc";
/// </example>
/// <param name="str">the String containing the substring, may be null</param>
/// <param name="open">the String before the substring, may be null</param>
/// <param name="close">the String after the substring, may be null </param>
/// <returns>the substring, null if no match</returns>
public static string SubstringBetween(this string str, string open, string close)
{
if (str == null || open == null || close == null)
return null;
string result = SubstringBefore(SubstringAfter(str, open), close);
if (result == string.Empty && str == string.Empty && (open != string.Empty || close != string.Empty))
return null;
return result;
}
/// <summary>
/// Searches a String for substrings delimited by a start and end tag,
/// returning all matching substrings in an array.
/// </summary>
/// <remarks>
/// A null input String returns null. A null open/close returns null (no match).
/// An empty ("") open/close returns null (no match).
/// </remarks>
/// <example>
/// StringExtensions.SubstringsBetween("[a][b][c]", "[", "]") = [ "a", "b", "c" ];
/// StringExtensions.SubstringsBetween(null, "*", "*") = null;
/// StringExtensions.SubstringsBetween("*", null, "*") = null;
/// StringExtensions.SubstringsBetween("*", "*", null) = null;
/// StringExtensions.SubstringsBetween("", "[", "]") = []);
/// </example>
/// <param name="str">the String containing the substrings, null returns null, empty returns empty</param>
/// <param name="open">the String identifying the start of the substring, empty returns null</param>
/// <param name="close">the String identifying the end of the substring, empty returns null </param>
/// <returns>a String Array of substrings, or null if no match</returns>
public static string[] SubstringsBetween(this string str, string open, string close)
{
if (str == null || string.IsNullOrEmpty(open) || string.IsNullOrEmpty(close))
{
return null;
}
int strLen = str.Length;
if (strLen == 0)
{
return new string[]{};
}
int closeLen = close.Length;
int openLen = open.Length;
List<string> list = new List<string>();
int pos = 0;
while (pos < (strLen - closeLen))
{
int start = str.IndexOf(open, pos);
if (start < 0)
{
break;
}
start += openLen;
int end = str.IndexOf(close, start);
if (end < 0)
{
break;
}
list.Add(str.Substring(start, end - start));
pos = end + closeLen;
}
if (list.Count == 0)
{
return null;
}
return list.ToArray();
}
/// <summary>
/// Repeats the specified string n times.
/// </summary>
/// <param name="instr">The input string.</param>
/// <param name="n">The number of times input string should be repeated.</param>
/// <returns></returns>
public static string Repeat(this string str, int n)
{
if (string.IsNullOrEmpty(str))
return str;
var result = new StringBuilder(str.Length * n);
return result.Insert(0, str, n).ToString();
}
#endregion
#region - Boolean Check Methods -
/// <summary>
/// Checks if the String can be converted into a numeric value.
/// </summary>
/// <remarks>null or an empty string will return false.</remarks>
/// <param name="str">the String to check, may be null </param>
/// <returns>true if it can be parsed into a numeric (decimal) value</returns>
public static bool IsNumeric(this string str)
{
if (string.IsNullOrEmpty(str))
return false;
decimal tmp;
return decimal.TryParse(str, out tmp);
}
/// <summary>
/// Checks if the String contains only unicode digits.
/// A decimal point is not a unicode digit and returns false.
/// </summary>
/// <remarks>null will return false. An empty String ("") will return true.</remarks>
/// <param name="str">the String to check, may be null </param>
/// <returns>true if only contains digits, and is non-null</returns>
public static bool IsDigit(this string str)
{
if (str == null)
return false;
int sz = str.Length;
for (int i = 0; i < sz; i++)
{
if (Char.IsDigit(str, i) == false)
return false;
}
return true;
}
/// <summary>
/// Checks if the String contains only unicode letters.
/// </summary>
/// <remarks>null will return false. An empty String ("") will return true.</remarks>
/// <param name="str">the String to check, may be null</param>
/// <returns>true if only contains letters, and is non-null</returns>
public static bool IsAlpha(this string str)
{
if (str == null)
return false;
int sz = str.Length;
for (int i = 0; i < sz; i++)
{
if (Char.IsLetter(str, i) == false)
return false;
}
return true;
}
/// <summary>
/// Checks if the String contains only unicode letters or digits.
/// </summary>
/// <remarks>null will return false. An empty String ("") will return true.</remarks>
/// <param name="str">the String to check, may be null</param>
/// <returns>true if only contains letters or digits, and is non-null</returns>
public static bool IsAlphaNumeric(this string str)
{
if (str == null)
return false;
int sz = str.Length;
for (int i = 0; i < sz; i++)
{
if (Char.IsLetterOrDigit(str, i) == false)
return false;
}
return true;
}
#endregion
}