I have a List which is returned to me from a 3rd party. Each string in the list ends in "mph" and I would like to remove the "mph". The obvious answer is to foreach over the list but I didn't know if there was a more efficient way to do it.
Thanks.
I have a List which is returned to me from a 3rd party. Each string in the list ends in "mph" and I would like to remove the "mph". The obvious answer is to foreach over the list but I didn't know if there was a more efficient way to do it.
Thanks.
You can use LINQ instead of a foreach loop:
list.Select( s => s.Substring(0, s.Length - 3) )
you can use LINQ for that purpose. Something like this might works :
var noMph = theList.Select(p => p.Replace("mph", "").ToList();
Well you can write
mylist.Select(s=>s.Substring(0, s.Length-3));//Can add .ToList() here
But that is using a loop. You don't have to write the foreach at least :)
substring to Substring :)