Short answer, no. You cannot "Remove" or "Add" to arrays directly.
You can however use Array.Resize
to resize the referenced array, or just straight up make a new one with a different element count.
This is not however answering the question you are ACTUALLY asking, for any given set of strings, how do you remove, conditionally, a portion of them.
I will assume going forwards you are/can use linq (System.Linq
) and we will use oras
.
Is it Time?
Given your example data, if you know they will all be "time" strings, you should parse to a strongly typed time object, probably TimeSpan
like this
var myTimeSpans = oras.Select(o => TimeSpan.Parse(o));
using your new list of TimeSpan
instances, you can then select only the elements that you do want using a Linq Where statement.
var myWantedTimespans = myTimeSpans.Where(ts => ts.TotalHours < 7 || ts.TotalHours > 10.5f);
This will get all TimeSpans where the total hours are lower than 7, or greater than 10.5. Modify as required for different conditional requirements.
you can then further utilise them in a strongly typed fashion in your new collection, or cast them back to string for whatever stringly typed purposes you may have.
Is it just strings?
If it's just arbitrary string values in an array, we can still get a theoretical Range
removed, but it's a bit more messy. If you take the strings at face value, you can get a Range
removed by using the base string comparer. For example:
string lower = "07:00AM";
string upper = "10:30AM";
var newArray = oras.Where(f => string.Compare(f, lower) < 0 || string.Compare(f, upper) > 0).ToArray();
this will remove a string Range
between 2 other string values. however this is taking all strings at face value, where their content is based on their character values individually and compared in that fashion. if there is any data that could be considered if it was in a strongly typed fashion this will not be considered in a string-only comparison.
Hope this helps.