c# change the string in a foreach string loop

后端 未结 2 2025
终归单人心
终归单人心 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] + "-";
    }
    
    0 讨论(0)
  • 2021-01-25 22:47

    Try this

    List<string> listString = new List<string>() { "1","2","3"};
    listString=listString.Select(x => x + "-").ToList();
    
    0 讨论(0)
提交回复
热议问题