32

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"))

1
  • 1
    In a single statement this would do the work: metricList = metricList.ConvertAll(s => s.Replace("XX", "1")); Commented Mar 12, 2014 at 11:06

3 Answers 3

49

You have a few issues here:

  • first, strings are immutable, so when you call .Replace you return a new string. Calling n.Replace doesn't modify n.
  • assigning to n in your anonymous function won't affect the value that's in your list.
  • regardless of the above, you can't change the content of your collection while enumerating it, because it'll invalidate the enumeration.

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:

var newList = metricList.Select(s => s.Replace("XX", "1")).ToList();
Sign up to request clarification or add additional context in comments.

2 Comments

Since he stated that he's using a generic list I would suggest ConvertAll(), because it does not iterate twice over the collection like Select & ToList together.
Select doesnt iterate the list it just defines and enumerable that the two list iterates over. One iteration
16

Problem: You aren't doing anything with the Replaced strings.
You could easily do this, using a simple loop:

C#

for(int i = 0; i < metricList.Count; i++)
{
    metricList[i] = metricList[i].Replace("XX", "1");
}

VB.NET

For i As Integer = 0 To metricList.Count - 1
    metricList(i) = metricList(i).Replace("XX", "1")
Next

Code iterates through all strings in metricList and replaces XX for 1, 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#

var newList = metricList.Select(x => x.Replace("XX", "1")).ToList();

VB.NET

Dim newList = metricList.Select(Function(x) x.Replace("XX", "1")).ToList()

Don't forget to add a reference to linq at the top of your class:

C#

using System.Linq;

VB.NET

Imports System.Linq

1 Comment

The "ForEach" in your Linq version should be a "Select"
1

You need to assign result of String.Replace method. So your func should return something or use instead of foreach select

2 Comments

You notice the cause here, but don't offer an option that solves it.
It is the option. You need to assign result of String.Replace method and use select means in code - .Select(x => x.Replace("a","b"))

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.