diff --git a/src/ArrayExtensions/ArrayExtensions.csproj b/src/ArrayExtensions/ArrayExtensions.csproj index ff96387..5117bd9 100644 --- a/src/ArrayExtensions/ArrayExtensions.csproj +++ b/src/ArrayExtensions/ArrayExtensions.csproj @@ -12,7 +12,7 @@ array, extensions LICENSE.txt README.md - 1.2 + 1.2.1 true LICENSE.txt README.md diff --git a/src/ArrayExtensions/Extensions/StringArrayExtensions.cs b/src/ArrayExtensions/Extensions/StringArrayExtensions.cs index 5fd6904..58cf512 100644 --- a/src/ArrayExtensions/Extensions/StringArrayExtensions.cs +++ b/src/ArrayExtensions/Extensions/StringArrayExtensions.cs @@ -7,42 +7,48 @@ public static class StringArrayExtensions /// /// The array to check. /// True if atleast one item is null or empty. - public static bool AnyNullOrEmpty(this string[] arr) => arr.Any(string.IsNullOrEmpty); + public static bool AnyNullOrEmpty(this string[] arr) + => arr.Any(string.IsNullOrEmpty); /// /// Checks if any item is null or whitespace in the array. /// /// The array to check. /// True if atleast one item is null or empty. - public static bool AnyNullOrWhiteSpace(this string[] arr) => arr.Any(string.IsNullOrWhiteSpace); + public static bool AnyNullOrWhiteSpace(this string[] arr) + => arr.Any(string.IsNullOrWhiteSpace); /// /// Trims all items in the array /// /// The array to trim all items. /// Array with trimmed items. - public static string[] TrimAll(this string[] arr) => arr.Select(s => s.Trim()).ToArray(); + public static string[] TrimAll(this string[] arr) + => arr.Select(s => s.Trim()).ToArray(); /// /// Removes all empty and null items from the array. /// /// The array to remove null or empty items from. /// Array without null or empty values. - public static string[] RemoveNullOrEmpty(this string[] arr) => arr.Where(s => !string.IsNullOrEmpty(s)).ToArray(); + public static string[] RemoveNullOrEmpty(this string[] arr) + => arr.Where(s => !string.IsNullOrEmpty(s)).ToArray(); /// /// Removes all whitespace and null items from the array. /// /// The array to remove whitespace or empty items from. /// Array without null or empty values. - public static string[] RemoveNullOrWhiteSpace(this string[] arr) => arr.Where(s => !string.IsNullOrWhiteSpace(s)).ToArray(); + public static string[] RemoveNullOrWhiteSpace(this string[] arr) + => arr.Where(s => !string.IsNullOrWhiteSpace(s)).ToArray(); /// /// Checks if the array has duplicate values. /// /// The array to check. /// True if any duplicate values present. - public static bool HasDuplicates(this string[] arr) => arr.GroupBy(s => s).Any(g => g.Count() > 1); + public static bool HasDuplicates(this string[] arr) + => arr.GroupBy(s => s).Any(g => g.Count() > 1); /// /// Concatenate all strings with a given separator. @@ -50,5 +56,83 @@ public static class StringArrayExtensions /// The array to concatenate. /// The separator. /// Concatenated string from all array elements. - public static string ConcatenateWithSeparator(this string[] arr, string separator) => string.Join(separator, arr); + public static string ConcatenateWithSeparator(this string[] arr, string separator) + => string.Join(separator, arr); + + /// + /// Converts all strings in the array to uppercase. + /// + /// The array to convert. + /// Array with all uppercase strings. + public static string[] ToUpperCase(this string[] arr) + => arr.Select(s => s.ToUpper()).ToArray(); + + /// + /// Converts all strings in the array to lowercase. + /// + /// The array to convert. + /// Array with all lowercase strings. + public static string[] ToLowerCase(this string[] arr) + => arr.Select(s => s.ToLower()).ToArray(); + + /// + /// Reverses each string in the array. + /// + /// The array to reverse. + /// Array with reversed strings. + public static string[] ReverseEach(this string[] arr) + => arr.Select(s => new string(s.Reverse().ToArray())).ToArray(); + + /// + /// Filters strings that match a given pattern. + /// + /// The array to filter. + /// The pattern to match. + /// Array with strings that match the pattern. + public static string[] FilterByPattern(this string[] arr, string pattern) + => arr.Where(s => System.Text.RegularExpressions.Regex.IsMatch(s, pattern)).ToArray(); + + /// + /// Counts occurrences of a substring in all strings of the array. + /// + /// The array to check. + /// The substring to count. + /// Total occurrences of the substring. + public static int CountOccurrencesOfSubstring(this string[] arr, string substring) + => arr.Sum(s => s.Split(new[] { substring }, StringSplitOptions.None).Length - 1); + + /// + /// Replaces a substring in all strings of the array. + /// + /// The array to replace in. + /// The old substring. + /// The new substring. + /// Array with replaced substrings. + public static string[] ReplaceInAll(this string[] arr, string oldValue, string newValue) + => arr.Select(s => s.Replace(oldValue, newValue)).ToArray(); + + /// + /// Checks if all strings in the array are of a certain length. + /// + /// The array to check. + /// The length to check. + /// True if all strings have the specified length. + public static bool AllOfLength(this string[] arr, int length) + => arr.All(s => s.Length == length); + + /// + /// Gets the longest string from the array. + /// + /// The array to check. + /// The longest string. + public static string LongestString(this string[] arr) + => arr.OrderByDescending(s => s.Length).FirstOrDefault(); + + /// + /// Gets the shortest string from the array. + /// + /// The array to check. + /// The shortest string. + public static string ShortestString(this string[] arr) + => arr.OrderBy(s => s.Length).FirstOrDefault(); } diff --git a/src/ArrayExtensions/README.md b/src/ArrayExtensions/README.md index e256cdc..f6493af 100644 --- a/src/ArrayExtensions/README.md +++ b/src/ArrayExtensions/README.md @@ -7,7 +7,7 @@ A collection of useful extension methods for arrays in C#. - **Basic Operations**: Easily `Add`, `Insert`, `Remove`, and `Replace` items. - **Array Manipulation**: `Shuffle`, `Rotate`, `Chunk`, `Flatten`, and `Resize` your arrays. - **Array Analysis**: Check if arrays are `AllEqual`, `AnyNull`, `IsEmpty`, or even find the `MostCommon` element. -- **String Array Utilities**: Methods like `TrimAll`, `AnyNullOrEmpty`, and `ConcatenateWithSeparator`. +- **String Array Utilities**: Methods like `TrimAll`, `AnyNullOrEmpty`, `ConcatenateWithSeparator` and filters. - **DateTime Array Utilities**: Filter by `Weekdays`, `Weekends`, `Holidays`, and more. - **Safe Operations**: Safely `Get`, `Set`, and `FindIndices` without worrying about out-of-range errors. - **Functional Programming**: Use `ForEach` to apply actions directly on array elements. @@ -46,6 +46,17 @@ bool hasEmptyOrNull = fruits.AnyNullOrEmpty(); // String utilities string[] trimmedFruits = { " apple ", "banana ", " cherry", " date " }; trimmedFruits = trimmedFruits.TrimAll(); +fruitsWithValues = fruitsWithValues.RemoveNullOrEmpty(); // Removes empty and null values, resulting in { "apple", "cherry" } + +string[] fruitsWithSpaces = { "apple", " ", "cherry", null }; +fruitsWithSpaces = fruitsWithSpaces.RemoveNullOrWhiteSpace(); // Removes whitespace-only and null values, resulting in { "apple", "cherry" } + +bool hasDuplicateFruits = fruits.HasDuplicates(); // Checks if there are any duplicate fruit names +string fruitSentence = fruits.ConcatenateWithSeparator(", "); // Joins all fruit names with a comma separator +string[] fruitsWithPattern = fruits.FilterByPattern("^a.*"); // Filters fruits that start with the letter 'a' +bool allOfLengthFive = fruits.AllOfLength(5); // Checks if all fruit names have a length of 5 +string longestFruit = fruits.LongestString(); // Gets the longest fruit name +string shortestFruit = fruits.ShortestString(); // Gets the shortest fruit name // DateTime utilities DateTime[] holidays = { new DateTime(2023, 12, 25), new DateTime(2023, 1, 1) };