c# change the string in a foreach string loop

后端 未结 2 2027
终归单人心
终归单人心 2021-01-25 22:22

I\'m trying to cycle through strings in a list with a foreach loop, but I don\'t know how to change the item that\'s being referenced - How can I change s in the li

2条回答
  •  迷失自我
    2021-01-25 22:41

    You can do this with Linq quite easily:

    var newStringList = myStringList
        .Select(s => s + "-")
        .ToList();
    

    If you really want to modify the existing list, you can use a classic for loop, for example:

    for (var i = 0; i < myStringList.Count; i++)
    {
        myStringList[i] = myStringList[i] + "-";
    }
    

提交回复
热议问题