C # remove rows from array

How can I remove any rows from an array and use only integers instead

string[] result = col["uncheckedFoods"].Split(','); 

I have

 [0] = on; // remove this string [1] = 22; [2] = 23; [3] = off; // remove this string [4] = 24; 

I want to

 [0] = 22; [1] = 23; [2] = 24; 

I tried

 var commaSepratedID = string.Join(",", result); var data = Regex.Replace(commaSepratedID, "[^,0-9]+", string.Empty); 

But there is a comma before the first element, is there a better way to delete lines?

+6
source share
3 answers

This selects all rows that can be parsed as int

 string[] result = new string[5]; result[0] = "on"; // remove this string result[1] = "22"; result[2] = "23"; result[3] = "off"; // remove this string result[4] = "24"; int temp; result = result.Where(x => int.TryParse(x, out temp)).ToArray(); 
+10
source

To support double I would do something like this:

 public static bool IsNumeric(string input, NumberStyles numberStyle) { double temp; return Double.TryParse(input, numberStyle, CultureInfo.CurrentCulture, out temp); } 

and then

 string[] result = new string[] {"abc", "10", "4.1" }; var res = result.Where(b => IsNumeric(b, NumberStyles.Number)); // res contains "10" and "4.1" 
0
source

try it

  dynamic[] result = { "23", "RT", "43", "67", "gf", "43" }; for(int i=1;i<=result.Count();i++) { var getvalue = result[i]; int num; if (int.TryParse(getvalue, out num)) { Console.Write(num); Console.ReadLine(); // It a number! } } 
0
source

Source: https://habr.com/ru/post/1012364/


All Articles