I've looked around but can't find anything that has helped me. I have the following issue - I have a string array that contains:
[0] = "2.4 kWh # 105.00 c/kWh"
where [0] is the index of the array. I need to split it by a space, so that I can have several smaller arrays. So it should look like:
[0] will contain 2.4
[1] will contain kWh
[2] will contain #
[3] will contain 105.00
[4] will contain c/mWh
I've tried several solutions but none works. Any assistance would be highly appreciated.
Reference
string s = "2.4 kWh # 105.00 c/kWh";
string[] words = s.Split(new char [] {' '}); // Split string on spaces.
foreach (string word in words)
{
Console.WriteLine(word);
}
Then you can get the console output as
2.4
kWh
#
105.00
c/mWh
We'll use string[] strings = new[] { "2.4 kWh # 105.00 c/kWh", "this is a test" }; as an example of your array.
This is how you can put it all into one array. I've kept it as an IEnumerable<T> to keep that benefit, but feel free to append .ToArray().
public IEnumerable<string> SplitAll(IEnumerable<string> collection)
{
return collection.SelectMany(c => c.Split(' '));
}
Here, this would evaluate to { "2.4", "kWh", "#", "105.00", "c/kWh", "this", "is", "a", "test" }.
Or if I'm misunderstanding you and you actually do want an array of arrays,
public IEnumerable<string[]> SplitAll(IEnumerable<string> collection)
{
return collection.Select(c => c.Split(' '));
}
Here, { { "2.4", "kWh", "#", "105.00", "c/kWh" }, { "this", "is", "a", "test" } }.
Or if I'm totally misunderstanding you and you just want to split the one string, that's even easier, and I've already shown it, but you can use string.Split.
This will give you a two dimensional array (array of string arrays):
var newArr = strArr.Select(s => s.Split(' ').ToArray()).ToArray();
for example:
string[] strArr = new string[] { "2.4 kWh # 105.00 c/kWh", "Hello, world" };
var newArr = strArr.Select(s => s.Split(' ').ToArray()).ToArray();
for (int i = 0; i < newArr.Length; i++)
{
for(int j = 0; j < newArr[i].Length; j++)
Console.WriteLine(newArr[i][j]);
Console.WriteLine();
}
// 2.4
// c/kWh
// #
// 105.00
// kWh
//
// Hello,
// world
Related
I have two different text files and I have to find 10 longest words that are in both of them. I have to print the list of those words out and write the frequency - how many times they are repeated in those separate files. The problem I have with my current code is that it finds the words, but when it comes to frequency - it combines the frequency count. How can I change the code to know the frequency count for separate files?
Here is my code for finding words that are in both text files:
public static Dictionary<string, int> PopularWords(string data1, string data2, char[] punctuation)
{
string[] book1 = data1.Split(punctuation, StringSplitOptions.RemoveEmptyEntries);
string[] book2 = data2.Split(punctuation, StringSplitOptions.RemoveEmptyEntries);
Dictionary<string, int> matches = new Dictionary<string, int>();
for (int i = 0; i < book1.Length; i++)
{
if (matches.ContainsKey(book1[i]))
{
matches[book1[i]]++;
continue;
}
for (int j = 0; j < book2.Length; j++)
{
if (book1[i] == book2[j])
{
if (matches.ContainsKey(book1[i]))
{
matches[book1[i]]++;
} else
{
matches.Add(book1[i], 2);
}
}
}
}
return matches;
And here is my code for reading and printing:
public static void ProcessPopular(string data, string data1, string results)
{
char[] punctuation = { ' ', '.', ',', '!', '?', ':', ';', '(', ')', '\n' };
string lines = File.ReadAllText(data, Encoding.UTF8);
string lines2 = File.ReadAllText(data1, Encoding.UTF8);
var popular = PopularWords(lines, lines2, punctuation);
KeyValuePair<string, int>[] popularWords = popular.ToArray();
Array.Sort(popularWords, (x, y) => y.Key.Length.CompareTo(x.Key.Length));
using (var writerF = File.CreateText(results))
{
int foundWords = 0;
writerF.WriteLine("{0, -25} | {1, -35} | {2, -35}", "Longest words", "Frequency in 1 .txt file", "Frequency in 2 .txt file");
writerF.WriteLine(new string('-', 101));
// not finished
}
}
Here's my take on this:
public static Dictionary<string, Dictionary<string, int>> PopularWords(string data1, string data2, char[] punctuation)
{
string[] book1 = data1.Split(punctuation, StringSplitOptions.RemoveEmptyEntries);
string[] book2 = data2.Split(punctuation, StringSplitOptions.RemoveEmptyEntries);
return
Enumerable
.Concat(
book1.Select(x => (word: x, book: "book1")),
book2.Select(x => (word: x, book: "book2")))
.ToLookup(x => x.word, x => x.book)
.OrderByDescending(x => x.Key.Length)
.Take(10)
.ToDictionary(x => x.Key, x => x.GroupBy(y => y).ToDictionary(y => y.Key, y => y.Count())); ;
}
If I start with this data:
char[] punctuation = new char[] { ' ', ',', '.', '?', '-', ':' };
string data1 = "I have two different text files and I have to find 10 longest words that are in both of them. I have to print the list of those words out and write the frequency - how many times they are repeated in those separate files. The problem I have with my current code is that it finds the words, but when it comes to frequency - it combines the frequency count. How can I change the code to know the frequency count for separate files?";
string data2 = "This solution is more general: it works whatever number of files you wish to process. This is an extremely raw query that could be separated in smaller queries, but it gives the logical basis. Other requirements, like only 10 words or minimum word length etc can be easily applied. Please do mind that this a bare-bone example, without any safety checks. It also omits reading data from files. The problem I have with my current code is that it finds the words, but when it comes to frequency - it combines the frequency count. How can I change the code to know the frequency count for separate files?";
I get this result:
"requirements": { "book2" = 1 }
"different": { "book1" = 1 }
"frequency": { "book1" = 4, "book2" = 3 }
"extremely": { "book2" = 1 }
"separated": { "book2" = 1 }
"repeated": { "book1" = 1 }
"separate": { "book1" = 2, "book2" = 1 }
"combines": { "book1" = 1, "book2" = 1 }
"solution": { "book2" = 1 }
"whatever": { "book2" = 1 }
To simplify, if performance is not the key here, I would go this way:
public static void Method()
{
var a = "A deep blue raffle, very deep and blue, raffle raffle. An old one was there";
var b = "deep blue raffle, very very very long and blue, raffle RAFFLE. A new one was there";
char[] punctuation = { '.', ',', '!', '?', ':', ';', '(', ')', '\n' };
var fileOne = new string(a.Where(c => punctuation.Contains(c) is false).ToArray()).Split(" ");
var fileTwo = new string(b.Where(c => punctuation.Contains(c) is false).ToArray()).Split(" ");
var duplicates = fileOne.Intersect(fileTwo, StringComparer.OrdinalIgnoreCase);
var result = new List<(int, int, string)>(duplicates.Count());
foreach(var duplicat in duplicates)
{
result.Add((fileOne.Count(x => x.Equals(duplicat, StringComparison.OrdinalIgnoreCase)), fileTwo.Count(x => x.Equals(duplicat, StringComparison.OrdinalIgnoreCase)), duplicat));
}
foreach (var val in result)
{
Output.WriteLine($"Word: {val.Item3} | In file one: {val.Item1} | In file two: {val.Item2}");
}
}
This will give you the result of
Word: A | In file one: 1 | In file two: 1
Word: deep | In file one: 2 | In file two: 1
Word: blue | In file one: 2 | In file two: 2
Word: raffle | In file one: 3 | In file two: 3
Word: very | In file one: 1 | In file two: 3
Word: and | In file one: 1 | In file two: 1
Word: one | In file one: 1 | In file two: 1
Word: was | In file one: 1 | In file two: 1
Word: there | In file one: 1 | In file two: 1
Other requirements, like only 10 words or minimum word length etc can be easily applied.
Please do mind that this a bare-bone example, without any safety checks. It also omits reading data from files.
EDIT I was not very pleased with my original solution, so I reworked it. I abandonned one thing I liked in my previous solution: the fact that it didn't depend on an external list of punctuation characters, but that this list was generated by the query itself. But it made the query more complicated and long.
In case you would be curious about a different coding style, here is a solution using Linq.
This solution is more general: it works whatever number of files you wish to process.
This is a Linqpad query that you can run directly via copy/paste, but you need to provide the text files of course:
// Choose here how many different words you want.
var resultCount = 10;
// Add as many files as needed.
var Files = new List<string>
{
#"C:\Temp\FileA.txt",
#"C:\Temp\FileB.txt",
#"C:\Temp\FileC.txt",
};
char[] punctuation = { '.', ',', '!', '?', ':', ';', '(', ')', '\n', '"', ' ' };
// Perform the calculation.
var LongestCommonWords = Files
.SelectMany(f => File.ReadAllText(f)
.Split(punctuation, StringSplitOptions.TrimEntries)
.ToLookup(w => ( word: w.ToLower(), fileName: f))
)
.ToLookup(e => e.Key.word)
.Where(g => g.Count() == Files.Count())
.OrderByDescending(g => g.Key.Length)
.Take(resultCount); // Take only the desired amount (10 for instance)
// Display the results.
foreach (var word in LongestCommonWords)
{
var occurences = string.Join(" / ", word.Select(g => $"{Path.GetFileName(g.Key.fileName)} - {g.Count()}"));
Console.WriteLine($"{word.Key} - {occurences}");
}
Here is an output obtained with the content of three Wikipedia pages:
contribution - FileA.txt - 9 / FileB.txt - 1 / FileC.txt - 5
subsequently - FileA.txt - 2 / FileB.txt - 1 / FileC.txt - 1
introduction - FileA.txt - 1 / FileB.txt - 4 / FileC.txt - 3
alternative - FileA.txt - 2 / FileB.txt - 1 / FileC.txt - 1
independent - FileA.txt - 5 / FileB.txt - 3 / FileC.txt - 3
significant - FileA.txt - 2 / FileB.txt - 1 / FileC.txt - 3
established - FileA.txt - 1 / FileB.txt - 1 / FileC.txt - 1
outstanding - FileA.txt - 1 / FileB.txt - 3 / FileC.txt - 3
programming - FileA.txt - 1 / FileB.txt - 2 / FileC.txt - 4
university - FileA.txt - 44 / FileB.txt - 17 / FileC.txt - 7
I'm trying to merge several values of diffrent lists into one line.
for example:
list A = [1,2,3,4,5,6,7,8,9]
list B = [A,B,C,D]
list C = [!,?,-]
then ill go with a loop through all lists and the output should be:
line = [1,A,!]
line = [2,B,?]
line = [3,C,-]
line = [4,D,NULL]
line = [5,NULL, NULL]
line = [6 ,NULL ,NULL]...
The result will be added into one object
So far I tried to iterate through my lists with foreach loops but after I debugging it's clear that my approach cant work:
foreach (var item in list1){
foreach (var item2 in list2){
foreach (var item3 in list3){
string line = makeStringFrom(item, item2, item3);
}
}
}
But I dont know how to make it work.
You can also use LINQ functions.
var listA = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
var listB = new List<string> { "A", "B", "C", "D" };
var listC = new List<string> { "!", "?", "-" };
var result = Enumerable.Range(0, Math.Max(Math.Max(listA.Count, listB.Count), listC.Count))
.Select(i => new
{
a = listA.ElementAtOrDefault(i),
b = listB.ElementAtOrDefault(i),
c = listC.ElementAtOrDefault(i)
}).ToList();
foreach (var item in result)
{
Console.WriteLine("{0} {1} {2}", item.a, item.b, item.c);
}
Result:
1 A !
2 B ?
3 C -
4 D
5
6
7
8
9
The general method would be:
Find the maximum length of all of the lists
Then create a loop to go from 0 to the max length-1
Check if each list contains that index of the item, and if so,
retrieve the value, otherwise return null
Build your line from those values
You can use this:
var A = new List<string>() { "1", "2", "3", "4", "5", "6", "7", "8", "9" };
var B = new List<string>() { "A", "B", "C", "D" };
var C = new List<string>() { "!", "?", "-"};
var lists = new List<List<string>>() { A, B, C };
int count = 0;
foreach ( var list in lists )
count = Math.Max(count, list.Count);
var result = new List<List<string>>();
for ( int index = 0; index < count; index++ )
{
var item = new List<string>();
result.Add(item);
foreach ( var list in lists )
item.Add(index < list.Count ? list[index] : null);
}
foreach ( var list in result )
{
string str = "";
foreach ( var item in list )
str += ( item == null ? "(null)" : item ) + " ";
str.TrimEnd(' ');
Console.WriteLine(str);
}
We create a list of the lists so you can use that for any number of lists.
Next we take the max count of these lists.
Then we parse them as indicated by the algorithm:
We create a new list.
We add this list to the result that is a list of lists.
We add in this new list each of others lists items while taking null is no more items available.
You can use a StringBuilder if you plan to manage several and big lists to optimize memory strings concatenation.
Fiddle Snippet
Output
1 A !
2 B ?
3 C -
4 D (null)
5 (null) (null)
6 (null) (null)
7 (null) (null)
8 (null) (null)
9 (null) (null)
I have a array of strings having values
string[] words = {"0B", "00", " 00", "00", "00", "07", "3F", "14", "1D"};
I need it to convert into array of ulong
ulong[] words1;
How should I do it in c#
I think I should add some background.
The data in the string is coming from textbox and I need to write the content of this textbox in the hexUpDown.Value parameter.
var ulongs = words.Select(x => ulong.Parse(x, NumberStyles.HexNumber)).ToArray();
If you need to combine the bytes into 64 bit values then try this (assumes correct endieness).
string[] words = { "0B", "00", " 00", "00", "00", "07", "3F", "14", "1D" };
var words64 = new List<string>();
int wc = 0;
var s = string.Empty;
var results = new List<ulong>();
// Concat string to make 64 bit words
foreach (var word in words)
{
// remove extra whitespace
s += word.Trim();
wc++;
// Added the word when it's 64 bits
if (wc % 4 == 0)
{
words64.Add(s);
wc = 0;
s = string.Empty;
}
}
// If there are any leftover bits, append those
if (!string.IsNullOrEmpty(s))
{
words64.Add(s);
}
// Now attempt to convert each string to a ulong
foreach (var word in words64)
{
ulong r;
if (ulong.TryParse(word,
System.Globalization.NumberStyles.AllowHexSpecifier,
System.Globalization.CultureInfo.InvariantCulture,
out r))
{
results.Add(r);
}
}
Results:
List<ulong>(3) { 184549376, 474900, 29 }
I have this string:
string Text = "{1}[56](17)(20)(13)(14)[895](11)(20)[3](8)(12)(3)[19](1)(2)(13)(7)(6)";
and i have to return this:
Array ( [Type] => 1
[Items] => Array ( [56] => Array ( [1] => 17
[2] => 20
[3] => 13
[4] => 14 )
[895] => Array ( [1] => 11
[2] => 20 )
[3] => Array ( [1] => 8
[2] => 12
[3] => 3 )
[19] => Array ( [1] => 1
[2] => 2
[3] => 13
[4] => 7
[5] => 6 )
)
)
How can i make this? I succeded in php but in c# i can't find the solution. I've tried first with dictionary, but i just can't reach it. I just don't know how to make a dictionary with a key with multiple values witch also have multiple values. So far i did this:
var Menu_Matrix = new Dictionary<string, string>();
var Menu_Items = new Dictionary<string, List<string>>();
char[] sep1 = { '{', '}' };
char[] sep2 = { '[', ']' };
char[] sep3 = { '(', ')' };
string[] Menu_Id = new string [10];
string[] Category_Id = new string[20];
Menu_Id = Text.Split(sep1);
Menu_Matrix.Add("Type", Menu_Id[1]);
Category_Id = Menu_Id[2].Split(sep2);
int cat_len = 0;
cat_len = Category_Id.Length;
for (int i = 1; i < cat_len;i++)
{ int pos = 0;
if(Category_Id[i+1].IndexOf('(')!=-1)
pos=i+1;
if(pos>i)
{ var item = new List<string>();
string[] Item_id = new string[20];
Item_id = Category_Id[pos].Split(sep3);
for (int j = 1; j < Item_id.Length;j++ )
if(Item_id[j]!="")
item.Add(Item_id[j]);
Menu_Items.Add(Category_Id[i], item);
}
i = pos;
}
}
return Menu_Items;
and the result is:
[56] => Array ( [1] => 17
[2] => 20
[3] => 13
[4] => 14 )
[895] => Array ( [1] => 11
[2] => 20 )
[3] => Array ( [1] => 8
[2] => 12
[3] => 3 )
[19] => Array ( [1] => 1
[2] => 2
[3] => 13
[4] => 7
[5] => 6 )
Hope you know what i wanna say and help me please! I don't care what i use : dictionary, jagged array or multidimensional array .
I thought a Dictionary<string, Dictionary<string, List<int>>> would be a good structure to store this information in.
The key to the outer Dictionary would be the values of "{#}"
The key to the inner Dictionary would be the values of "[#]"
The values in the List, in the inner Dictionary, would be the values of "(#)", but without the parentheses
To parse out this information, I thought a combinations of Regex.Splits to get the outer and inner keys along with a Regex.Match to get the values of the inner keys was a good approach.
Code Sample:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
public class Program
{
public static void Main()
{
string Text = "{1}[56](17)(20)(13)(14)[895](11)(20)[3](8)(12)(3)[19](1)(2)(13)(7)(6){2}[99](1)(2)(3)";
// Split out pairs
// 0: {#}
// 1: [#](#)..(n)
string[] splits = Regex.Split(Text, "({\\d+})").Where(split => !String.IsNullOrEmpty(split)).ToArray();
Dictionary<string, Dictionary<string, List<int>>> items = new Dictionary<string, Dictionary<string, List<int>>>();
for (int i = 0; i < splits.Length; i += 2)
{
// splits[i] is {#} which will make the key for this part of the Dictionary
items.Add(splits[i], new Dictionary<string, List<int>>());
items[splits[i]] = new Dictionary<string, List<int>>();
// Split out sub pairs
// 0: [#]
// 1: (#)..(n)
string[] subSplits = Regex.Split(splits[i + 1], "(\\[\\d+\\])").Where(subSplit => !String.IsNullOrEmpty(subSplit)).ToArray();
for (int j = 0; j < subSplits.Length; j += 2)
{
// subSplits[j] is [#] which will make the key for the inner Dictionary
items[splits[i]].Add(subSplits[j], new List<int>());
// subSplits[j + 1] is all of the (#) for each [#]
// which we'll add to the List of the inner Dictionary
Match m = Regex.Match(subSplits[j + 1], "(\\d+)");
while (m.Success)
{
items[splits[i]][subSplits[j]].Add(Convert.ToInt32(m.Groups[0].ToString()));
m = m.NextMatch();
}
}
}
// Print the keys of the Dictionary, the keys of the inner Dictionary, the values of the inner Dictionary
foreach (string key in items.Keys)
{
Console.WriteLine("Key: {0}", key);
foreach (string subKey in items[key].Keys)
{
Console.WriteLine("\t SubKey: {0}", subKey);
Console.WriteLine("\t\t Value: {0}", String.Join(", ", items[key][subKey]));
}
}
}
}
Results:
Key: {1}
SubKey: [56]
Value: 17, 20, 13, 14
SubKey: [895]
Value: 11, 20
SubKey: [3]
Value: 8, 12, 3
SubKey: [19]
Value: 1, 2, 13, 7, 6
Key: {2}
SubKey: [99]
Value: 1, 2, 3
See working sample here... https://dotnetfiddle.net/Zt5gXc
You could consider something like this:
string text = "{1}[56](17)(20)(13)(14)[895](11)(20)[3](8)(12)(3)[19](1)(2)(13)(7)(6)";
string[] tokens = text.Split(new Char[] { '}', ']', ')' });
char symbol;
int value;
Dictionary<int, Dictionary<int, List<int>>> data = new Dictionary<int, Dictionary<int, List<int>>>();
Dictionary<int, List<int>> items = null;
List<int> leaves = null;
foreach (string token in tokens) {
if (token.Length == 0) break;
symbol = token[0];
value = Int32.Parse(token.Substring(1));
switch (symbol) {
case '{':
items = new Dictionary<int, List<int>>();
data.Add(value, items);
break;
case '[':
leaves = new List<int>();
items.Add(value, leaves);
break;
case '(':
leaves.Add(value);
break;
}
}
foreach (int type in data.Keys)
{
Console.WriteLine("Type => {{{0}}}", type);
Console.WriteLine("\tItems =>");
items = data[type];
foreach (int item in items.Keys)
{
Console.WriteLine("\t\t[{0}] =>", item);
leaves = items[item];
for (int i = 0; i < leaves.Count; i += 1) {
Console.WriteLine("\t\t\t[{0}] => ({1})", i, leaves[i]);
}
}
}
See this working at DotNetFiddle.net
This produces the output:
Type => {1}
Items =>
[56] =>
[0] => (17)
[1] => (20)
[2] => (13)
[3] => (14)
[895] =>
[0] => (11)
[1] => (20)
[3] =>
[0] => (8)
[1] => (12)
[2] => (3)
[19] =>
[0] => (1)
[1] => (2)
[2] => (13)
[3] => (7)
[4] => (6)
Here's a run-down of how it works:
Split all the numbers on the closing mark (curly brace, square bracket, or parenthesis). This creates an array like { "{1", "[56", "(17", ... "(6", "" }.
Create a variable for each level of the data structures that represents the last object at that depth of the hierarchy.
Quit early if we're on the last, blank, token.
Iterate over the split-out tokens and save the opening mark that represents the data structure hierarchy level into symbol and the the numerical value into value.
For each symbol, take the appropriate action.
Huge note: this code strongly expects the input string to be well-formed. If, for example, you passed in the string "{1}(17)" everything would blow up because no intermediate [56] was present to populate the leaves variable with new List<int> that the ( code expects to be instantiated already.
Edit: I have tried the Take/Skip method but I get the following error:
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<string>' to
'string[]'. An explicit conversion exists (are you missing a cast?)
I do not know what I am doing wrong because I copied Saeed's code.
I have a string array (containing anywhere from 20 to 300 items) and I want to split it into 2 separate arrays, from the middle of the first one.
I know how I can do this using a for loop but I would like to know if there was a faster/better way of doing it. I also need to be able to correctly split an array even if it has an odd number of items, eg:
string[] words = {"apple", "orange", "banana", "pear", "lemon"};
string[] firstarray, secondarray;
SplitArray(words, out firstarray, out secondarray); // Or some other function
// firstarray has the first 3 of the items from words, 'apple', 'orange' and 'banana'
// secondarray has the other 2, 'pear' and 'lemon'
You can use linq:
firstArray = array.Take(array.Length / 2).ToArray();
secondArray = array.Skip(array.Length / 2).ToArray();
Why this works, despite the parity of the original array size?
The firstArray takes array.Length / 2 elements, and the second one skips the first array.Length / 2 elements, it means there isn't any conflict between these two arrays. Of course if the number of elements is odd we cannot split the array into two equal size parts.
If you want to have more elements in the first half (in the odd case), do this:
firstArray = array.Take((array.Length + 1) / 2).ToArray();
secondArray = array.Skip((array.Length + 1) / 2).ToArray();
string[] words = {"apple", "orange", "banana", "pear", "lemon"};
int mid = words.Length/2;
string[] first = words.Take(mid).ToArray();
string[] second = words.Skip(mid).ToArray();
If you don't want to/can't use LINQ you can simply do:
string[] words = { "apple", "orange", "banana", "pear", "lemon" };
string[] firstarray, secondarray;
int mid = words.Length / 2;
firstarray = new string[mid];
secondarray = new string[words.Length - mid];
Array.Copy(words, 0, firstarray, 0, mid);
Array.Copy(words, mid, secondarray, 0, secondarray.Length);
A more generalized approach that will split it into as many parts as you specify:
public static IEnumerable<IEnumerable<T>> Split<T>(this IEnumerable<T> list, int parts)
{
return list.Select((item, index) => new {index, item})
.GroupBy(x => (x.index + 1) / (list.Count()/parts) + 1)
.Select(x => x.Select(y => y.item));
}
*Edited Thanks skarmats
string[] words = { "apple", "orange", "banana", "pear", "lemon" };
var halfWay = words.Length/2;
var firstHalf = words.Take(halfWay);
var secondHalf = words.Skip(halfWay);
You can achive that quite easily using range notation:
var x = new[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
var pivot = x.Length / 2;
var p1 = x[..pivot];
var p2 = x[pivot..];
Just in case someone wants to use a function instead:
static void Main(string[] args)
{
string[] ar = { "apple", "orange", "banana", "pear", "lemon" };
int half = ar.Length / 2;
// Console.WriteLine(string.Join(',', Split(ar,0, half)));
Console.WriteLine(string.Join(',', Split(ar,half, ar.Length)));
Console.ReadKey();
}
public static IEnumerable<T> Split<T>(IEnumerable<T> items, int start, int end)
{
return items.Skip(start).Take(end);
}