Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
288 views
in Technique[技术] by (71.8m points)

c# - 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"

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, 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()));
                                    }
                                }
                            }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Just do something like:

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());

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...