如何从字符串中删除逗号分隔的值? [英] How to remove comma separated value from a string?
问题描述
我想从字符串中删除逗号分隔的值.
I want to remove a comma separated value from the string..
假设我有一个像这样的字符串
suppose I have a string like this
string x="r, v, l, m"
我想从上述字符串中删除r,并像这样修改字符串
and i want to remove r from the above string, and reform the string like this
string x="v, l, m"
从上面的字符串中,我想删除逻辑抛出的任何值并重新格式化该字符串.它应该删除该值和旁边的逗号并重新设置字符串...
from the above string i want to remove any value that my logic throw and reform the string. it should remove the value and comma next to it and reform the string...
以下内容专门针对我的代码.我想删除从逻辑中获得的任何值,我想删除它并在其旁边添加逗号,并在删除的项目上重新设置字符串而没有空白.
The below is specific to my code.. I want to remove any value that I get from the logic, I want to remove it and comma next to it and reform the string with no empty space on the deleted item.. How can I achieve this?
offIdColl = my_Order.CustomOfferAppliedonOrder.TrimEnd(',');
if (offIdColl.Split(',').Contains(OfferID.ToString()))
{
// here i want to perform that operation.
}
Tombala,我像这样应用它,但是它不起作用..它返回true
}
Tombala, i applied it like this but it doesn't work..it returns true
if (!string.IsNullOrEmpty(my_Order.CustomOfferAppliedonOrder))
{
offIdColl = my_Order.CustomOfferAppliedonOrder.TrimEnd(',');
if (offIdColl.Split(',').Contains(OfferID.ToString()))
{
string x = string.Join(",", offIdColl.Split(new char[] { ',' },
StringSplitOptions.RemoveEmptyEntries).ToList().Remove(OfferID.ToString()));
}
}
}
推荐答案
只需执行以下操作:
List<String> Items = x.Split(",").Select(i => i.Trim()).Where(i => i != string.Empty).ToList(); //Split them all and remove spaces
Items.Remove("v"); //or whichever you want
string NewX = String.Join(", ", Items.ToArray());
这篇关于如何从字符串中删除逗号分隔的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!