-5

For example

 private string[] oras = { "07:00AM", "07:30AM", "08:00AM", "08:30AM", "09:00AM", "10:00AM", " 10:30AM", "11:00AM", "11:30AM", "12:00PM", "12:30PM", "01:00PM", "01:30PM", "02:00PM", "02:30PM", "03:00PM", "03:30PM", "04:00PM", "04:30PM", "05:00PM", "05:30PM", "06:00PM", "06:30PM", "07:00PM", "07:30PM", "08:00PM" };

i want to remove items between "7:00AM" and "10:30AM"

Papa Joe
  • 1
  • 3
  • 2
    the easiest solution is to create TImeSpan objects from these hours and the use linq – miechooy Oct 03 '17 at 08:47
  • 1
    Array is fixed size, you can't remove elements from it. What do you need is create a method which converts array to collection, remove the specified value range & return a new array minus removed items. – Tetsuya Yamamoto Oct 03 '17 at 08:48
  • can you link me some tuitorials sir. Thank you so much – Papa Joe Oct 03 '17 at 08:49
  • Your vales are strings. Now, me (a human) can understand what you mean by "between 7:00AM and 10:30AM" but the computer just sees a string. You must first tell the computer that your strings represent times, and then we're a bit closer. – Jamiec Oct 03 '17 at 08:50
  • `oras.ToList()` then declare another list e.g. `var excluded = new List { "07:00AM", "07:30AM", "08:00AM", "08:30AM", "09:00AM", "10:00AM", " 10:30AM" }`, then use `oras.Except(excluded).ToArray()` (see https://stackoverflow.com/questions/8032394/how-to-delete-a-chosen-element-in-array for detail). – Tetsuya Yamamoto Oct 03 '17 at 08:51
  • create a list & use RemoveAt() to remove items. – Ghost Developer Oct 03 '17 at 08:51
  • Thank you sir i will try all of your suggesstions – Papa Joe Oct 03 '17 at 08:55

6 Answers6

3

Steps:

  • Convert to TimeSpan;
  • Filter where the time span doesn't meet your criteria;
  • Optionally convert back to string and/or an array.

Code:

var orasTs = oras.Select(o => ParseTimeSpan(o))
                 .Where(ts => !(ts.TotalHours >= 7 && ts.TotalHours <= 10.5f))
                 .ToArray();

    private static TimeSpan ParseTimeSpan(string t)
    {
        int hoursToAdd = 0;

        if (t.EndsWith("AM"))
        {
            hoursToAdd = 0;
        }
        else
        {
            hoursToAdd = 12;
        }

        return TimeSpan.Parse(t.Substring(0, 5)).Add(TimeSpan.FromHours(hoursToAdd));
    }
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
1

You can try this. What I am doing here is that I am converting first the time and select based on your condition. Then return the result that should not match on the first result:

string[] oras = { "07:00AM", "07:30AM", "08:00AM", "08:30AM", "09:00AM", "10:00AM", " 10:30AM", "11:00AM", "11:30AM", "12:00PM", "12:30PM", "01:00PM", "01:30PM", "02:00PM", "02:30PM", "03:00PM", "03:30PM", "04:00PM", "04:30PM", "05:00PM", "05:30PM", "06:00PM", "06:30PM", "07:00PM", "07:30PM", "08:00PM" };

var res = oras.Where(c => DateTime.Parse(c) >= DateTime.Parse("07:00AM") && DateTime.Parse(c) <= DateTime.Parse("10:30AM"))
                .ToArray();

var result = oras.Where(b => !res.Any(c => c == b)).ToArray();
Willy David Jr
  • 8,604
  • 6
  • 46
  • 57
1

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.

Skintkingle
  • 1,579
  • 3
  • 16
  • 28
0

Looks like you are having fun here :)
Here's my realization:

string format = "hh:mmtt";
DateTime excludeStart = DateTime.ParseExact("07:00AM", format, CultureInfo.InvariantCulture),
    excludeEnd = DateTime.ParseExact("10:30AM", format, CultureInfo.InvariantCulture);
var result = oras
     .Select(str => DateTime.ParseExact(str.Trim(), format, CultureInfo.InvariantCulture))
     .Where(ts => ts < excludeStart || ts > excludeEnd)
     .Select(ts => ts.ToString("hh:mmtt"))
     .ToArray();

You can run it here

Pavel Agarkov
  • 3,633
  • 22
  • 18
0

Another possible variant:

  • Convert your list items to DateTime
  • Calculate count of items for given range
  • Calculate the range's start element index
  • Use RemoveRange() method, after making list from your array
  • Convert back to string array

    string[] oras = { "07:00AM", "07:30AM", "08:00AM", "08:30AM", "09:00AM", "10:00AM", " 10:30AM", "11:00AM", "11:30AM", "12:00PM", "12:30PM", "01:00PM", "01:30PM", "02:00PM", "02:30PM", "03:00PM", "03:30PM", "04:00PM", "04:30PM", "05:00PM", "05:30PM", "06:00PM", "06:30PM", "07:00PM", "07:30PM", "08:00PM" };
    
    int elementsCount = oras.Select(DateTime.Parse)
                    .Count(c => c >= DateTime.Parse("07:00AM")
                             && c <= DateTime.Parse("10:30AM"));
    
    int startIndex = Array.IndexOf(oras, "07:00AM");
    
    List<string> orasList = oras.ToList();
    orasList.RemoveRange(startIndex, elementsCount);
    oras = orasList.ToArray();
    
SᴇM
  • 7,024
  • 3
  • 24
  • 41
-1

If you can use List, you can use List.RemoveRange (MSDN)

List<string> listHour = new List<string>();
foreach(var item in oras){
    listHour.Add(item);
}
listHour.RemoveRange(1,4);
oras = listHour.ToArray();
EchoZulu
  • 71
  • 11