i have a string array with values { "01.0", "01.4", "01.5", "0.20", "02.5", "02.6", "03.0", "03.2" }
how do i filter integer values (1,2,3) from this using C#?
-
3What do you mean by filter? Do you mean select the values at index 1, 2, and 3?Chris Eberle– Chris Eberle2011-04-06 18:27:16 +00:00Commented Apr 6, 2011 at 18:27
-
I agree with @Chris. Need more information. How about only returning 1, 2, 3, from the array? Basically converting all of the numbers to integers and removing all the duplicates?Ryan Alford– Ryan Alford2011-04-06 18:28:59 +00:00Commented Apr 6, 2011 at 18:28
-
we should be able to select any value that is of the form 01.0 (which is 1), 02.0 (is 2), 03.0 ...and so on.user583126– user5831262011-04-06 18:29:02 +00:00Commented Apr 6, 2011 at 18:29
-
@user583126 - should the "0.20" entry be "02.0"? I made that assumption with my answer based on how you phrased your question. If that is not true, then Jon has the correct answer.NerdFury– NerdFury2011-04-06 18:52:35 +00:00Commented Apr 6, 2011 at 18:52
-
@NerdFury: basically i wanted to capture values with precision 0 (integers) and your logic works for me. i think i could have phrased my question a little more clearly.user583126– user5831262011-04-06 19:01:58 +00:00Commented Apr 6, 2011 at 19:01
Add a comment
|
4 Answers
First do a select to convert the string values to decimals. Then use the Remainder function to find which values have a zero remainder when divided by 1. That should get you only integer values.
var array = new[]{"01.0", "01.4", "01.5", "02.0", "02.5", "02.6", "03.0", "03.2"};
array.Select(i => Decimal.Parse(i)).Where(d => Decimal.Remainder(d, 1) == 0);
4 Comments
Jon
This will not work if the selected culture in the OS where the code runs does not use
. as the decimal delimiter. Also, decimal and Remainder are needlessly heavyweight tools for the job. And finally, you need to filter out duplicates.user583126
great NerdFury. i think i missed to think on the lines of computing remainder.
NerdFury
@Jon - I think the confusion is the "0.20" record. I assumed this was a typo due to the fact that the format was not consistent with the others and the way the question was phrased. The question does not say that duplicates need to be filtered out, but in your solution it is necessary. My solution can be made global just as easily as your solution was. And in terms of "heavyweight" - performance wise I did a test where I filtered the list 1000000 times. My solution was about 40ms faster each time.
Jon
Then I think it could be made faster still. Cheers! :)
Parse the strings to floats, select only those that are integers and cast off duplicate entries:
var input = new[] { "01.0", "01.4", "01.5", "0.20", "02.5",
"02.6", "03.0", "03.2" };
var integers = input.Select(i =>
double.Parse(i,
System.Globalization.CultureInfo.InvariantCulture))
.Where(d => d == (int)d)
.Distinct().ToArray();
Answer edited to take into account the OP's later comments.
1 Comment
user583126
thnx Jon. i like your answer as well but SO doesn't allow me to check 2 answers :)
You can use int.Parse(array[here]), as long as your array is a string, or convertible to a string.
2 Comments
Adam Lear
or int.TryParse() for an exception-free approach.
user583126
you will get FormatException "Input string was not in a correct format."
var array = new[] { "01.0", "01.4", "01.5", "02.0", "02.5", "02.6", "03.0", "03.2" };
int foo;
var integers = (from a in array where decimal.Remainder(decimal.Parse(a), 1) == 0 select (int)decimal.Parse(a));
foreach (var integer in integers)
{
Console.WriteLine(integer);
}
Console.ReadLine();