using System; using System.Collections.Generic; using System.Text.RegularExpressions; namespace Default { public class StringHelpers { // generics public static IEnumerable> Split(IEnumerable src, int size) => src.Where((x, i) => i % size == 0).Select((x, i) => src.Skip(i * size).Take(size)); public static IEnumerable Select(params T[] args) => args; public static List List(params T[] args) => args.ToList(); // string specific public static bool StrEquals(string a, string b) => string.Equals((a ?? "").Trim(), (b ?? "").Trim(), StringComparison.OrdinalIgnoreCase); public static bool FirstNonEmpty(params string[] args) => args.FirstOrDefault(x => !string.IsNullOrWhiteSpace(x)); public static bool AnyEmpty(params string[] args) => args.Any(x => string.IsNullOrWhiteSpace(x)); public static string LimitString(string str, int length) => str?.Substring(0, Math.Min(str.Length, length)); public static IEnumerable SplitSizes(string str, params int[] sizes) { var start = 0; return sizes.Select(s => { if (start + s > str.Length) return ""; var result = str.Substring(start, s); start += s; return result; }); } public static string JoinUrl(string base, params string[] parts) { var partList = new List(); partList.Add(base.TrimEnd('/')); foreach (string part in parts) { if (!String.IsNullOrEmpty(part)) partList.Add(part); } return String.Join("/", partList); } private static readonly Regex nonAsciiRegex = new Regex("[^ -~]+", RegexOptions.Compiled); public static string ReplaceNonAsciiChars(string input) { if (string.IsNullOrWhiteSpace(input)) return input; return nonAsciiRegex.Replace(input, ""); } public static string ShortenText(string caption, int length) { if (string.IsNullOrEmpty(caption) || length <= 0) return ""; if (caption.Length <= length) return caption; int count = length - 5; int lastIndex = caption.LastIndexOfAny(new char[] { ' ', ',', '.', '-' }, length - 1, count); if (lastIndex >= 0) return caption.Remove(lastIndex) + " ..."; else return caption.Remove(length - 4) + " ..."; } public static string ShortenTextSimple(string caption, int length) { if (string.IsNullOrEmpty(caption) || length <= 0) return ""; if (caption.Length <= length) return caption; return caption.Remove(length - 3) + "..."; } public static List SplitString(string str, int chunkSize) { if (string.IsNullOrWhiteSpace(str)) str = ""; var chunks = new List(); ; int stringLength = str.Length; for (int i = 0; i < stringLength; i += chunkSize) { if (i + chunkSize > stringLength) chunkSize = stringLength - i; chunks.Add(str.Substring(i, chunkSize)); } if (chunks.Count == 0) chunks.Add(""); return chunks; } public static string Right(string value, int length) => value.Substring(value.Length - length); public static string UppercaseFirst(string str) { if (string.IsNullOrEmpty(str)) return str; return char.ToUpper(str[0]) + str.Substring(1); } public static bool HasString(string src, string item) { return src?.IndexOf(item ?? "", StringComparison.OrdinalIgnoreCase) >= 0; } public static string Repeat(string value, int count) { return new StringBuilder(value.Length * count).Insert(0, value, count).ToString(); } } }