Files
Common-VS2019/SacsStandardBase/Base/Utils/StringListUtil.cs
T
2024-03-27 15:05:33 +00:00

191 lines
7.3 KiB
C#

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
//-------------------------------------------------------------------
/// <summary>Create list of strings with value repeated n times.</summary>
public static List<String> CreateList (String value, int repeat = 1)
{
List<String> values = new List<String>();
for (int i = 0; i < repeat; i++)
values.Add(value);
return values;
}
//-------------------------------------------------------------------
// Split string using delimiter and return list.
public static List<String> Split (String data, String delimiter = ",", bool trimSpaces = true, bool ignoreEmpty = false, bool ignoreDuplicates = false, bool caseSensitive = false, long maxItems = 0)
{
List<String> values = new List<String>();
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
//-------------------------------------------------------------------
/// <summary>Compare lists to verify that they contain the same value in the corresponding position.</summary>
public static bool ContentEquals (this IEnumerable<String> values, IEnumerable<String> 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;
}
/// <summary>Compare lists to verify that they contain the same case-insensitive value in the corresponding position.</summary>
public static bool ContentEqualsCI (this IEnumerable<String> values, IEnumerable<String> 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
//-------------------------------------------------------------------
/// <summary>Does the list have any entries.</summary>
public static bool IsEmpty (this ICollection<String> values)
{
return (values.Count == 0);
}
/// <summary>Case-insensitive contains.</summary>
public static bool ContainsCI (this IEnumerable<String> values, String value)
{
return values.Contains(value, StringComparer.OrdinalIgnoreCase);
}
//-------------------------------------------------------------------
// E x t r a c t i o n
//-------------------------------------------------------------------
/// <summary>
/// Remove the specified number of entries from the top of the list and return them.
/// <para>Original list retains the remaining items.</para>
/// </summary>
public static List<String> ExtractValues (this List<String> values, int numberToRemove)
{
List<String> extracted = new List<String>();
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
//-------------------------------------------------------------------
/// <summary>Remove all values that match, case-insensitive.</summary>
public static void RemoveAllCI (this List<String> values, String value)
{
values.RemoveAll(item => item.EqualsCI(value));
}
/// <summary>Remove all blank values with option to trim values before checking for blank.</summary>
public static void RemoveBlanks (this List<String> 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--;
}
}
}
/// <summary>Remove all duplicate values from the list, case-insensitive.</summary>
public static void RemoveDuplicatesCI (this List<String> 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--;
}
}
}
}
/// <summary>Remove all duplicate values from the list, case-insensitive.</summary>
public static void RemoveTrailingBlanks(this List<String> 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
//-------------------------------------------------------------------
/// <summary>
/// Bracket each entry in the list with the specified prefix and suffix (eg. '[' & ']').
/// <para>Options to trim values and ignore blank values.</para>
/// </summary>
public static List<String> Bracket (this IList<String> values, String prefix = "", String suffix = "", bool trimSpaces = true, bool ignoreEmpty = false)
{
List<String> results = new List<string>();
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<String> 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();
}
//-------------------------------------------------------------------
}
//=======================================================================
}