using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Sarand.Net.Standard.Base.Utils { //======================================================================= public static class StringListUtil { //------------------------------------------------------------------- // C r e a t i o n //------------------------------------------------------------------- /// Create list of strings with value repeated n times. public static List CreateList (String value, int repeat = 1) { List values = new List(); for (int i = 0; i < repeat; i++) values.Add(value); return values; } //------------------------------------------------------------------- // Split string using delimiter and return list. public static List Split (String data, String delimiter = ",", bool trimSpaces = true, bool ignoreEmpty = false, bool ignoreDuplicates = false, bool caseSensitive = false, long maxItems = 0) { List values = new List(); if (data == null) return values; foreach (String rawValue in data.Split(new string[] { delimiter }, StringSplitOptions.None)) { String value = rawValue; if (trimSpaces) value = value.Trim(); if (ignoreEmpty && value.IsEmpty()) continue; if (ignoreDuplicates && values.Contains(value, caseSensitive ? StringComparer.OrdinalIgnoreCase : StringComparer.Ordinal)) continue; values.Add(value); if (maxItems > 0 && values.Count >= maxItems) break; } return values; } //------------------------------------------------------------------- // C o m p a r i s o n //------------------------------------------------------------------- /// Compare lists to verify that they contain the same value in the corresponding position. public static bool ContentEquals (this IEnumerable values, IEnumerable target) { if (values.Count() != target.Count()) return false; var combinedValues = values.Zip(target, Tuple.Create); foreach (var combinedValue in combinedValues) if (!combinedValue.Item1.Equals(combinedValue.Item2)) return false; return true; } /// Compare lists to verify that they contain the same case-insensitive value in the corresponding position. public static bool ContentEqualsCI (this IEnumerable values, IEnumerable target) { if (values.Count() != target.Count()) return false; var combinedValues = values.Zip(target, Tuple.Create); foreach (var combinedValue in combinedValues) if (!combinedValue.Item1.EqualsCI(combinedValue.Item2)) return false; return true; } //------------------------------------------------------------------- // C o n t e n t s //------------------------------------------------------------------- /// Does the list have any entries. public static bool IsEmpty (this ICollection values) { return (values.Count == 0); } /// Case-insensitive contains. public static bool ContainsCI (this IEnumerable values, String value) { return values.Contains(value, StringComparer.OrdinalIgnoreCase); } //------------------------------------------------------------------- // E x t r a c t i o n //------------------------------------------------------------------- /// /// Remove the specified number of entries from the top of the list and return them. /// Original list retains the remaining items. /// public static List ExtractValues (this List values, int numberToRemove) { List extracted = new List(); while (values.Count > 0 && extracted.Count < numberToRemove) { String id = values.ElementAt(0); values.RemoveAt(0); extracted.Add(id); } return extracted; } //------------------------------------------------------------------- // C l e a n s i n g //------------------------------------------------------------------- /// Remove all values that match, case-insensitive. public static void RemoveAllCI (this List values, String value) { values.RemoveAll(item => item.EqualsCI(value)); } /// Remove all blank values with option to trim values before checking for blank. public static void RemoveBlanks (this List values, bool trimValues = true) { for (int idx = 0; idx < values.Count; idx++) { String value = values[idx]; if (trimValues) value = value.Trim(); if (StringUtil.IsEmpty(value)) { values.RemoveAt(idx); idx--; } } } /// Remove all duplicate values from the list, case-insensitive. public static void RemoveDuplicatesCI (this List values) { for (int idx = 0; idx < values.Count; idx++) { String value = values[idx]; for (int idx2 = idx + 1; idx2 < values.Count; idx2++) { String value2 = values[idx2]; if (value.EqualsCI(value2)) { values.RemoveAt(idx2); idx2--; } } } } /// Remove all duplicate values from the list, case-insensitive. public static void RemoveTrailingBlanks(this List values) { while (!values.IsEmpty() && StringUtil.IsBlank(values.Last())) values.RemoveAt(values.Count - 1); } //------------------------------------------------------------------- // M o d i f i c a t i o n //------------------------------------------------------------------- /// /// Bracket each entry in the list with the specified prefix and suffix (eg. '[' & ']'). /// Options to trim values and ignore blank values. /// public static List Bracket (this IList values, String prefix = "", String suffix = "", bool trimSpaces = true, bool ignoreEmpty = false) { List results = new List(); prefix = StringUtil.EmptyOnNull(prefix); suffix = StringUtil.EmptyOnNull(suffix); foreach (String rawValue in values) { String value = rawValue; if (trimSpaces) value = value.Trim(); if (ignoreEmpty && value.IsEmpty()) continue; results.Add(prefix + value + suffix); } return results; } //------------------------------------------------------------------- // C o n c a t e n a t i o n //------------------------------------------------------------------- public static String Concat (this IEnumerable values, String delimiter, String prefix = "", String suffix = "", bool trimSpaces = true, bool ignoreEmpty = false) { delimiter = StringUtil.EmptyOnNull(delimiter); prefix = StringUtil.EmptyOnNull(prefix); suffix = StringUtil.EmptyOnNull(suffix); StringBuilder output = new StringBuilder(); int valuesAdded = 0; foreach (String rawValue in values) { String value = rawValue; if (trimSpaces) value = value.Trim(); if (ignoreEmpty && value.IsEmpty()) continue; if (valuesAdded++ > 0) output.Append(delimiter); output.Append(prefix).Append(value).Append(suffix); } return output.ToString(); } //------------------------------------------------------------------- } //======================================================================= }