0

i have a string array with different latitudes and longitudes,i want to read only the digits and store it into a new string.

private static void Main(string[] args)
{

        string[] str_array = new string[2];
        str_array[0] = "lat = 30.25588 ; lon = 75.22558;";
        str_array[1] = "lat = 30.23088 ; lon = 46.22558;";
        str_array[2] = "lat = 30.24688 ; lon = 75.22758;";

        string result = "";
        string checkstring = ".0123456789";

        for (int i = 0; i <= str_array.Length; i++)
        {
            for (int j = 0; j <= str_array[i].Length; j++)
            {
                if (checkstring.Contains(str_array[i][j]))
                {
                    result += str_array[i][j].ToString();
                }
                else if (str_array[i][j] == ';')
                {
                    result += " ";
                }
            }
        }
        Console.WriteLine(result);
        Console.ReadLine();
Jibran Khan
  • 3,236
  • 4
  • 37
  • 50
neeraj
  • 1
  • 1
  • 3
  • 2
    `new string[2]` should be `new string[3]` – Chronicle Mar 03 '20 at 14:17
  • `i < str_array.Length` instead of `i <= str_array.Length`, same for `j < str_array[i].Length` – Dmitry Bychenko Mar 03 '20 at 14:18
  • I know the question has been closed, but if your strings maintain the same pattern, you can just do foreach(var str in str_array), then do .Split on a white space on str, and then just manually grab the values from the index, in your case it would be indexes 2 and 6. This would avoid any of that extra checking and nested for loops – SomeStudent Mar 03 '20 at 14:26

0 Answers0