I am trying to find a specific string in a property of a class and replace it with another string if found. I tried various methods using Linq but I am not able to replace it. In debug mode, when I monitor the object again, the replace command hasn't worked. Please help. Including a sample example below.
class Tryreplace
{
public string ClubName { get; set; }
}
List<Tryreplace> tryreplaces = new List<Tryreplace>();
Tryreplace tryreplace = new Tryreplace { ClubName = "Manchester United FC" };
Tryreplace tryreplace2 = new Tryreplace { ClubName = "Arsenal FC" };
tryreplaces.Add(tryreplace);
tryreplaces.Add(tryreplace2);
Tried 2 ways below, both didn't work...
tryreplaces.ForEach(x => x.ClubName.Replace("Manchester", "Newcastle"));
List<Tryreplace> tryreplaces2 = tryreplaces.Select(x => { x.ClubName.Replace("Manchester", "Newcastle"); return x; }).ToList();
After both, I only see Manchester and not Newcastle.
I would like to see replaced string Newcastle in the objects and not Manchester. Please help!