Parse int[] from "1,2,3" - c#

I have a multiselect dropdown called ID that submits ID=1,2,3 which I need parsed into an integer array to do a Contains() on in a filter method. At the moment I use:
string[] ids = Request["ID"].Split(',');
Which I don't really like because its slower to compare string than int. Any suggestions?

Request["ID"].Split(',').Select(x=>int.Parse(x)).ToArray();
Of course, this will throw if any of the resulting numeric strings are not "parseable" (does such a word exist?).

It depends on how many times you will look up in the array if converting to ints are faster or the string comparison is faster.
HashSet<int> ids = new HashSet<int>(from s in Request["ID"].Split(',')
select int.Parse(s));
But probably the fastest if you have many id:s will be to create a HashSet<string>:
HashSet<string> = new HashSet<string>(string[] ids = Request["ID"].Split(','));

int[] ids = Request["ID"].Split(',').Select(Convert.ToInt32).ToArray();

First:
string[] arr = Request["ID"].Split(',')
Then:
Array.ConvertAll(arr, s => Int32.Parse(s));
Array.ConvertAll(arr, Int32.Parse);
arr.Select(s => Int32.Parse(s));
arr.Select(Int32.Parse);
Then:
new HashSet<int>(result);
(the most fast container to perform Contains())
See also:
Convert string[] to int[] in one string of code using LINQ
Which one is faster in processing and conversion int.Parse(), int.TryParse(), Convert.Int32()

If you don't have linq, you can:
string[] ids = Request["ID"].Split(',');
int[] items = new int[ids.Length];
for(int i = 0; i<ids.Length; i++)
{
items[i] = int.Parse(ids[i]);
}

Related

remove elements from array until specific index

I have a string array, I need to delete all elements until a specific index or get a new array of all elements from my specific index. I'm looking for a system function without loops.
my code for example:
string []myArray = {"AAA","BBB","CCC","DDD","EEE","FFF","GGG","HHH"}
int myIndex = Array.IndexOf(myArray, "DDD");
needed output :
string []myNewArray = {"EEE","FFF","GGG","HHH"}
Just use Skip in Linq
string []myArray = {"AAA","BBB","CCC","DDD","EEE","FFF","GGG","HHH"}
int myIndex = Array.IndexOf(myArray, "DDD");
var newArray = myArray.Skip(myIndex+1);
Of course this means only that the loop is hidden from your view, but it exits nevertheless inside the Skip method.
Also, the code above, will return the whole array if the search for the string is unsuccessful.
You can use Linq's SkipWhile
string[] myArray = { "AAA", "BBB", "CCC", "DDD", "EEE", "FFF", "GGG", "HHH" };
var myNewArray = myArray.SkipWhile(x => x != "DDD").Skip(1).ToArray();
Arrays are a pretty "dumb" object, as they mostly just have methods that effectively describe themselves. What you'll want to do is make it Queryable (one of its methods), then use LINQ to do it.
string[] myarray = GimmeAStringArray();
int x = desiredIndexValue;
return myarray.AsQueryable().Where(t => myarray.IndexOf(t) > x);
You might not need to AsQueryable() it to do that.
there is another simple way to do that using arrayList:
you can use method arraylist.RemoveRange(start index, last index)
public static void Main()
{
string[] array = new string[] {"AAA","BBB","CCC","DDD","EEE","FFF","GGG","HHH"};
List<string> list = new List<string>(array);
list.RemoveRange(0,list.IndexOf("DDD")+1);
foreach (string str in list)
{
Console.WriteLine(str);
}
}
output of program will be :
EEE
FFF
GGG
HHH

How to convert string array to long array?

I am in bust at the moment, I have this string array:
string[] StringNum = { "4699307989721714673", "4699307989231714673", "4623307989721714673", "4577930798721714673" };
I need to convert them To long array data type in C#:
long[] LongNum= { 4699307989721714673, 4699307989231714673, 4623307989721714673, 4577930798721714673 };
But I have no idea how, is it even possible?
You could use simple Linq extension functions.
long[] LongNum = StringNum.Select(long.Parse).ToArray();
or you can use long.TryParse on each string.
List<long> results = new List<long>();
foreach(string s in StringNum)
{
long val;
if(long.TryParse(s, out val))
{
results.Add(val);
}
}
long[] LongNum = results.ToArray();
var longArray = StringNum.Select(long.Parse).ToArray();
It can probably be done in less code with Linq, but here's the traditional method: loop each string, convert it to a long:
var longs = new List<Long>();
foreach(var s in StringNum) {
longs.Add(Long.Parse(s));
}
return longs.ToArray();
If you are looking for the fastest way with smallest memory usage possible then here it is
string[] StringNum = { "4699307989721714673", "4699307989231714673", "4623307989721714673", "4577930798721714673" };
long[] longNum = new long[StringNum.Length];
for (int i = 0; i < StringNum.Length; i++)
longNum[i] = long.Parse(StringNum[i]);
Using new List<long>() is bad because every time it needs an expansion then it reallocates a lot of memory. It is better to use new List<long>(StringNum.Lenght) to allocate enough memory and prevent multiple memory reallocations. Allocating enough memory to list increases performance but since you need long[] an extra call of ToArray on List<> will do the whole memory reallocation again to produce the array. In other hand you know the size of output and you can initially create an array and do the memory allocation.
You can iterate over the string array and keep converting the strings to numeric using the long.Parse() function. Consider the code below:
string[] StringNum =
{ "4699307989721714673",
"4699307989231714673",
"4623307989721714673",
"4577930798721714673" };
long[] LongNum = new long[4];
for(int i=0; i<4; i++){
LongNum[i] = long.Parse(StringNum[i]);
}
This converts and stores each string as a long value in the LongNum array.

Convert IEnumerable<byte[]> to byte[]

var r = from s in tempResult
select Encoding.GetEncoding("iso-8859-1").GetBytes(s);
I understand, this returns IEnumerable<byte[]>, but I looking for LINQ way to convert the whole IEnumerable<byte[]> to byte[].
None of the answers provided so far will work, because they will convert the IEnumerable<byte[]> to byte[][]. If your goal is to take all of the arrays in the enumerable and produce one big array, try this:
byte[] result = r.SelectMany(i => i).ToArray();
See this ideone example.
Note that this is not the most efficient way to do this. It would be faster to convert the original query result to a list, then compute the sum of the array lengths. Once that is done, you can allocate the final array immediately, then make one more pass over the result list and copy each result array's contents into the larger array.
The above LINQ query definitely makes this task easy, but it will not be fast. If this code becomes a bottleneck in the application, consider rewriting it this way.
I might as well provide an example of a more efficient implementation:
public static T[] JoinArrays<T>(this IEnumerable<T[]> self)
{
if (self == null)
throw new ArgumentNullException("self");
int count = 0;
foreach (var arr in self)
if (arr != null)
count += arr.Length;
var joined = new T[count];
int index = 0;
foreach (var arr in self)
if (arr != null)
{
Array.Copy(arr, 0, joined, index, arr.Length);
index += arr.Length;
}
return joined;
}
Note that whatever enumerable you pass in will be enumerated twice, so it would be a good idea to pass in a list or an array instead of a query, if that query is expensive.
Are you sure that's what you want to do? This code already returns a byte array:
Encoding.GetEncoding("iso-8859-1").GetBytes(s);
In any case, if you want to convert the enumeration to an array, you would do so like this:
var myArray = (from s in tempResult
select Encoding.GetEncoding("iso-8859-1").GetBytes(s)).ToArray();
EDIT
After your edit, I see I've misunderstood what you're trying to accomplish. If I understand correctly now, you're trying to get a byte array containing concatenated strings in tempResult? I would so it like this:
var concatenated = String.Join("", tempResult.ToArray());
var byteArray = Encoding.GetEncoding("iso-8859-1").GetBytes(concatenated);
What about ToArray extension method?
byte[] array = r.SelectMany(a => a).ToArray();
var r = (from s in tempResult
select Encoding.GetEncoding("iso-8859-1").GetBytes(s)
).ToArray();

Splitting a string on a space and removing empty entries

I am splitting a string like this:
string[] elements = templine.Split
(space, StringSplitOptions.RemoveEmptyEntries);
How can I get every element from templine except for [1] and assign it to string[] elements?
string[] elements = templine.Split(space).Where((s, i) => i != 1).ToArray();
Lots of crazy LINQ examples here. This should probably be more efficient, if that matters to you:
public static T[] SkipElement<T>(this T[] source, int index)
{
// Leaving out null/bounds checking for brevity.
T[] array = new T[source.Length - 1];
Array.Copy(source, 0, array, 0, index);
Array.Copy(source, index + 1, array, index, source.Length - index - 1);
return array;
}
With this you could do:
string[] elements = templine.Split(space, StringSplitOptions.RemoveEmptyEntries);
elements = elements.SkipElement(1);
List<String> elements = templine.Split
(space, StringSplitOptions.RemoveEmptyEntries).
ToList().
RemoveAt(1);
If you feel the need to go back to an array...
string[] elements = templine.Split
(space, StringSplitOptions.RemoveEmptyEntries).
ToList().
RemoveAt(1).
ToArray();
If you can use Linq, you can probably do:
IEnumerable<string> elements = templine.Split(...).Take(1).Skip(1);
string[] elements = templine.Split
(space, StringSplitOptions.RemoveEmptyEntries).Where(s => s != "[1]").ToArray();
if you mean you'd like to remove the second element
string[] elements = templine.Split
(space, StringSplitOptions.RemoveEmptyEntries).Where((s, i) => i != 1).ToArray();
Using Linq, you can just do
IEnumerable<String> elements =
empline.Split(space, StringSplitOptions.RemoveEmptyEntries);
String[] result =
elements
.Take(1)
.Concat(elements
.Skip(2))
.ToArray();
After your code above:
elements = elements.Take(1).Concat(elements.Skip(2)).ToArray();
One thing I want to point out: Take and Skip will not throw errors even if the original elements is too short; they'll simply produce empty IEnumerables (which can be safely concatenated, and whose methods can be safely called).
Well, you could either ask your brother Jon
Or you could do something like this:
var elements = templine.Split(space, StringSplitOptions.RemoveEmptyEntries).Skip(1).ToArray();
This will give you an array that contains all but the first element. If you want the first, but not the second (as your question sort of suggests), you could do this:
var allElements = templine.Split(space, StringSplitOptions.RemoveEmptyEntries);
var someElements = allElements.Take(1).Concat(allElements.Skip(2)).ToArray();

Convert List of longs to string array

What is the best way to convert a List of primitive longs to an array of strings?
I think I'm looking for something fancier than writing my own loop, etc..
This should do the trick:
string[] arr = list.Select(l => l.ToString()).ToArray();
This is worth checking against the performance of a select statement.
string[] result = Array.ConvertAll(list.ToArray(), i => i.ToString());

Categories

Resources