GithubHelp home page GithubHelp logo

construct0-forks / arrayextensions Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tim-maes/arrayextensions

0.0 0.0 0.0 56 KB

Collection of extension methods on arrays. Also available as NuGet package.

License: MIT License

C# 100.00%

arrayextensions's Introduction

ArrayExtensions

A collection of useful extension methods for arrays in C#.

Features

  • 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, 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.
  • ... and many more

Installation

Install via NuGet:

Install-Package ArrayExtensions

Or just copy whatever you need from this repository :)

Usage

Some examples:

using ArrayExtensions;

// Basic array of fruits
string[] fruits = { "apple", "banana", "cherry" };

// Add items
fruits = fruits.Add("date").AddRange("fig", "grape");

// Manipulate array
fruits = fruits.InsertAt(2, "blueberry").RemoveAt(3).Shuffle();

// Analyze array
bool allSame = fruits.AllEqual();
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) };
var holidayDates = dates.FilterHolidays(holidays);
var weekdays = dates.FilterWeekdays();

// Safe operations
var fruit = fruits.SafeGet(10, "unknown");
var indices = fruits.FindIndices(f => f.StartsWith("a"));

// Functional programming
fruits.ForEach(Console.WriteLine);

// More advanced operations
var chunkedFruits = fruits.Chunk(2);  // Splits the array into chunks of 2
var rotatedFruits = fruits.RotateLeft(2);  // Rotates the array to the left by 2 positions
var lastThreeFruits = fruits.LastN(3);  // Retrieves the last 3 fruits
var distinctFruits = fruits.DistinctValues();  // Gets distinct fruit values
var mostCommonFruit = fruits.MostCommon();  // Finds the most common fruit

// DateTime specific operations
DateTime[] dateArray = { new DateTime(2023, 1, 1), new DateTime(2023, 2, 14), new DateTime(2023, 12, 25) };
var range = dateArray.DateRange();  // Gets the range between the earliest and latest date
var closestToNewYear = dateArray.ClosestTo(new DateTime(2023, 1, 15));  // Finds the date closest to Jan 15, 2023

License

MIT

arrayextensions's People

Contributors

tim-maes avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.