How to remove comma separated value from a string?

后端 未结 10 1852
你的背包
你的背包 2021-01-03 00:13

I want to remove a comma separated value from the string..

suppose I have a string like this

string x=\"r, v, l, m\"

and i want to

10条回答
  •  春和景丽
    2021-01-03 00:48

    So you want to delete an item (or replace it with a nother value) and join the string again with comma without space?

    string x = "r, v, l, m,";
    string value = "v";
    string[] allVals = x.TrimEnd(',').Split(new []{','}, StringSplitOptions.RemoveEmptyEntries);
    // remove all values:
    x = string.Join(",", allVals.Where(v => v.Trim() != value));
    // or replace these values
    x = string.Join(",", allVals.Select(v => v.Trim() == value ? "new value" : v));
    

提交回复
热议问题