-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"

7
  • 2
    the easiest solution is to create TImeSpan objects from these hours and the use linq Commented Oct 3, 2017 at 8: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. Commented Oct 3, 2017 at 8:48
  • can you link me some tuitorials sir. Thank you so much Commented Oct 3, 2017 at 8: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. Commented Oct 3, 2017 at 8:50
  • oras.ToList() then declare another list e.g. var excluded = new List<string> { "07:00AM", "07:30AM", "08:00AM", "08:30AM", "09:00AM", "10:00AM", " 10:30AM" }, then use oras.Except(excluded).ToArray() (see stackoverflow.com/questions/8032394/… for detail). Commented Oct 3, 2017 at 8:51

6 Answers 6

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));
    }
Sign up to request clarification or add additional context in comments.

5 Comments

thanks sir it is more realistic
I feel like your Where linq will get you all the values you dont want, not the list of values minus the values you dont want.
@Skintkingle Notice the ! before the condition.
Ah yeah! I notice it now! +1 for you!
it sucks when your just a student and your project given by your prof. is class scheduling :(
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();

1 Comment

Glad to help :)
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.

2 Comments

Thanks for your effort sir. I really Appreciate it thanks a lot
I've just added a String-only comparson, if your time data is just an example and you actually need stringy ranges instead.
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

1 Comment

thanks @pavel Agarkov
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();
    

Comments

-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();

2 Comments

Thanks for your answer i will try this too
You're Welcome :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.