0

I am parsing a set of coordinates from an XML file. Each node will have coordinates like:

-82.5,34.1,0.000 -82.6,34.2,0.000

In the code below, the coords_raw variable is already assigned the above value and I am trying to split into array lnglatset --which does look okay.

    string[] lnglatset = raw_coords.Split(' ');//will yield like     [0]=-82.00,34.00,00000 // Will need to get rid of the last set of zeros
    foreach (string lnglat in lnglatset)
    {
       Console.WriteLine(lnglat);//-82.5,34.1,0.000; looks fine
    }  

From the above, the final value needed would be:

     coords = "-82.5 34.1, -82.6 34.2";//note the space between lng/lat

But how do remove the junk values of 0.000 from each element of the array and put a space, instead of a comma between the lng and lat values in each element? I have tried some remove() function on lnglat but that was not allowed within the foreach loop. Thanks!

4
  • Is your junk data always "0.000"? Is it always at the end? Is there ever a valid coordinate element of "00.0" or "0.00"? I suspect the end solution is going to involve parsing the string into useful elements, scrapping the original string, then constructing a new string out of the collection of elements, however there's probably a more elegant solution. Regex, maybe...? Commented Feb 13, 2015 at 19:44
  • JD, yes, 0.000 is always at end. Always junk. Commented Feb 13, 2015 at 19:49
  • I notice in your example code above, the last string in the original array is "00000" (first comment), whereas in the output, which evidently "looks fine" it's represented as "0.000". Typo on your part, or is something else going on? At any rate, it looks like the Take solution below will do the trick. And I learned something, an added bonus. Commented Feb 13, 2015 at 19:53
  • Yes, typo. These are not exact values but there are indeed 0.000000 in every element's end. Commented Feb 13, 2015 at 19:59

5 Answers 5

3

You can take all parts except the last one using Take method:

var parts =  raw_coords.Split(' ')
             .Select(x => x.Split(','))
             .Select(x => string.Join(" ", x.Take(x.Length - 1)));

var result = string.Join(",", parts);
Sign up to request clarification or add additional context in comments.

1 Comment

BTW, I think this solution requires using System.Linq;
1

In a single line :

String result = String.Join(" ",  raw_coords.Split(' ', ',')
.Select(i => double.Parse(i))
.Where(i => i != 0).Select( i => i.ToString()));

it removes each 0.000 element and removes the space and the comma.

1 Comment

Thanks. This would have worked--selman's answer came first when I look. Your help much appreciated.
0

You can't alter the members of IEnumerable during a ForEach. Instead, you can just skip the last member when splitting the raw coordinate input.

raw_coords.split(' ').Take(2).ToArray()

3 Comments

I think your's would have worked; the other guy gave fuller answer. I appreciate the help.
this will not give the expected result, you are not splitting on comma.and also you are just getting the same parts back, if you concatenate them you will get the same string back. so this code practically does nothing.
The point was to demonstrate the use of the Take method to only grab the first two elements of an IEnumerable, not to provide drop-in ready code, but yes you're correct.
0

Like others mentioned, you can not modify iterating variable with foreach. I learnt it the hard way and ended up using simple "for" loop instead of foreach:

for(int index=0; i<lnglatset.length-2; i++)
{
}

Comments

0

You can use IEnumerable.Last() extension method from System.Linq.

 string lastItemOfSplit = aString.Split(new char[] {@"\"[0], "/"[0]}).Last();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.