WGet.NET 4.4.1
A WinGet wrapper library for .Net.
Loading...
Searching...
No Matches
ArrayExtensions.cs
1//--------------------------------------------------//
2// Created by basicx-StrgV //
3// https://github.com/basicx-StrgV/ //
4//--------------------------------------------------//
5using System;
6
8{
13 internal static class ArrayExtensions
14 {
30 public static T[] Add<T>(this T[] array, T value)
31 {
32 if (value is null)
33 {
34 return array;
35 }
36
37 Array.Resize(ref array, array.Length + 1);
38
39 //Add the new line to the new enty in the main array.
40#if NETCOREAPP3_1_OR_GREATER
41 array[^1] = value;
42#elif NETSTANDARD2_0
43 array[array.Length - 1] = value;
44#endif
45
46 return array;
47 }
48
67 public static T[] RemoveRange<T>(this T[] array, int index, int count)
68 {
69 if (array.Length - 1 < index)
70 {
71 return array;
72 }
73
74 //Only copy the needed range of the input array to the new array.
75 T[] newArray = new T[array.Length - count];
76 for (int i = 0; i < array.Length; i++)
77 {
78 if (i < index)
79 {
80 newArray[i] = array[i];
81 }
82 else if (i + count < array.Length)
83 {
84 newArray[i] = array[i + count];
85 }
86 }
87
88 return newArray;
89 }
90
103 public static int GetEntryContains(this string[] array, string value)
104 {
105 int index = -1;
106
107 for (int i = 0; i < array.Length; i++)
108 {
109 if (array[i].Contains(value))
110 {
111 index = i;
112 break;
113 }
114 }
115
116 return index;
117 }
118
128 public static string[] RemoveEmptyEntries(this string[] array)
129 {
130 string[] newArray = Array.Empty<string>();
131
132 for (int i = 0; i < array.Length; i++)
133 {
134 if (!string.IsNullOrWhiteSpace(array[i]))
135 {
136 newArray = newArray.Add(array[i]);
137 }
138 }
139
140 return newArray;
141 }
142 }
143}