I want to update all strings in a List, that do not start with "http://" to start with "http://"
In a foreach I would do something like this:
url = url.StartsWith("http://") ? url : url.Insert(0, "http://");
Just use a regular for
loop - that's the simplest way of modifying the collection:
for (int i = 0; i < list.Count; i++)
{
string url = list[i];
if (!url.StartsWith("http://"))
{
list[i] = "http://" + url;
}
}
If you're happy to create a new collection, it's simple:
var modifiedList = list.Select(url => url.StartsWith("http://") ? url : "http://" + url)
.ToList();
See more on this question at Stackoverflow