How to edit an element in the List (Of Strings) The method ( For Each Or Linq)

Mansour_Dalir 2,036 Reputation points
2023-07-28T05:45:57.6+00:00
        Dim lstStt As New List(Of String)
        lstStt.Add("A")
        lstStt.ForEach(Sub(d) d = "") ' No Edit

OR

       For Each d In lstStt
            d = ""  'No Edit
        Next

If such a code is not practical, leave me a comment that this is not possible. thank.

Developer technologies | VB
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.6K Reputation points
    2023-07-28T09:02:50.43+00:00

    In VB and C# it is not possible to modify the strings (which are “immutable”), but you can replace the items of the list with other values. For example, lstStt(0) = "" will replace the first item with "". To modify more items, use a For loop, or maybe something like this:

    Enumerable.Range(0, lstStt.Count).All(Function(i)
                                                lstStt(i) = "" : Return True
                                            End Function)
    

    You can also create a new list:

    lstStt = Enumerable.Repeat("", lstStt.Count).ToList
    

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.