using System; using System.Collections.Generic; using System.Linq; using ICD.Common.Properties; namespace ICD.Common.Utils.Extensions { public static class StringExtensions { /// /// Returns the first appearance of an item from the given sequence. /// /// /// /// /// [PublicAPI] public static int IndexOf(this string extends, IEnumerable items, out string first) { if (extends == null) throw new ArgumentNullException("extends"); if (items == null) throw new ArgumentNullException("items"); int index = -1; first = null; foreach (string item in items) { int thisIndex = extends.IndexOf(item, StringComparison.Ordinal); if (thisIndex == -1) continue; if (index != -1 && thisIndex >= index) continue; index = thisIndex; first = item; if (index == 0) break; } return index; } /// /// Returns true if the string starts with the given character. /// /// /// /// [PublicAPI] public static bool StartsWith(this string extends, char character) { if (extends == null) throw new ArgumentNullException("extends"); return extends.Length > 0 && character == extends[0]; } /// /// Returns true if the string ends with the given character. /// /// /// /// [PublicAPI] public static bool EndsWith(this string extends, char character) { if (extends == null) throw new ArgumentNullException("extends"); return extends.Length > 0 && character == extends[extends.Length - 1]; } /// /// Splits the string by the given delimiter, returning up to the given number of substrings. /// E.g. "a:b:c".Split(':', 2) returns ["a", "b:c"] /// /// /// /// /// public static IEnumerable Split(this string extends, char delimeter, int count) { if (extends == null) throw new ArgumentNullException("extends"); if (count < 1) throw new ArgumentException("Value must be greater or equal to 1", "count"); return SplitIterator(extends, delimeter, count); } /// /// Splits the string by the given delimiter, returning up to the given number of substrings. /// E.g. "a:b:c".Split(':', 2) returns ["a", "b:c"] /// /// /// /// /// private static IEnumerable SplitIterator(string value, char delimeter, int count) { while (count > 1) { int index = value.IndexOf(delimeter); if (index < 0) break; yield return value.Substring(0, index); value = value.Substring(index + 1); count--; } yield return value; } /// /// Splits a string into chunks of the given length. /// /// /// /// [PublicAPI] public static IEnumerable Split(this string extends, int chunkSize) { if (extends == null) throw new ArgumentNullException("extends"); if (chunkSize <= 0) throw new InvalidOperationException("chunkSize must be greater than 0"); return Enumerable.Range(0, (int)Math.Ceiling(extends.Length / (double)chunkSize)) .Select(i => extends.Substring(i * chunkSize, Math.Min(chunkSize, extends.Length - (i * chunkSize)))); } /// /// Removes whitespace from the string. /// /// /// [PublicAPI] public static string RemoveWhitespace(this string extends) { if (extends == null) throw new ArgumentNullException("extends"); return new string(extends.Where(c => !char.IsWhiteSpace(c)).ToArray()); } /// /// Removes all occurances of the given string. /// /// /// /// public static string Remove(this string extends, string other) { if (extends == null) throw new ArgumentNullException("extends"); if (other == null) throw new ArgumentNullException("other"); if (string.IsNullOrEmpty(other)) return extends; int index; while ((index = extends.IndexOf(other, StringComparison.Ordinal)) >= 0) extends = extends.Remove(index, other.Length); return extends; } /// /// Removes all occurances the given characters from the string. /// /// /// /// [PublicAPI] public static string Remove(this string extends, IEnumerable characters) { if (extends == null) throw new ArgumentNullException("extends"); if (characters == null) throw new ArgumentNullException("characters"); var cSet = characters.ToIcdHashSet(); return new string(extends.Where(c => !cSet.Contains(c)).ToArray()); } /// /// Returns true if the string only contains numbers. /// /// /// [PublicAPI] public static bool IsNumeric(this string extends) { if (extends == null) throw new ArgumentNullException("extends"); return extends.AnyAndAll(char.IsDigit); } /// /// Returns true if the string contains the given character. /// /// /// /// public static bool Contains(this string extends, char character) { if (extends == null) throw new ArgumentNullException("extends"); return extends.Contains(character.ToString()); } /// /// Generates a hashcode that is consistent between program executions. /// /// /// public static int GetStableHashCode(this string extends) { unchecked { int hash1 = 5381; int hash2 = hash1; for (int i = 0; i < extends.Length && extends[i] != '\0'; i += 2) { hash1 = ((hash1 << 5) + hash1) ^ extends[i]; if (i == extends.Length - 1 || extends[i + 1] == '\0') break; hash2 = ((hash2 << 5) + hash2) ^ extends[i + 1]; } return hash1 + (hash2 * 1566083941); } } } }