I have a collection of strings which contain values like "goalXXvalue,goalXXLength,TestXX". It is a List(of String) I thought I would be able to loop through each item and replace the XX value which I've tried with the method below but the values don't change. Where am I going wrong? Thanks
metricList.ForEach(Function(n) n.Replace("XX", "1"))
Problem: You aren't doing anything with the Replaced strings.
You could easily do this, using a simple loop:
C#
VB.NET
Code iterates through all strings in
metricList
and replacesXX
for1
, it then stores the values back at the correct place in the list, what you aren't doing in your code...Or using Linq:
C#
VB.NET
Don't forget to add a reference to linq at the top of your class:
C#
VB.NET
You have a few issues here:
.Replace
you return a new string. Callingn.Replace
doesn't modifyn
.n
in your anonymous function won't affect the value that's in your list.Since it seems you're changing every string in your list, it seems unnecessary to try to modify the collection in-place. Therefore, the succint solution would be to use Linq would to create a new list:
You need to assign result of String.Replace method. So your func should return somthing or use instead of foreach select