Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
291 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 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...