How to remove comma separated value from a string?

后端 未结 10 1802
你的背包
你的背包 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 01:01
    var l = x.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
    l.Remove(OfferID.ToString());
    x = string.Join(",", l);
    

    Edit: Sorry, you're right. Remove doesn't return the original list. You need multiple statements. But you don't need to trim the end "," implicitly. You can remove that statement from your code as well as the check to see if the item is there or not. The Remove will take it out if it was found or simply return false if it was not found. You don't have to check existence. So remove the TrimEnd from the first and get rid of the second line below:

    offIdColl = my_Order.CustomOfferAppliedonOrder; //.TrimEnd(',');
    //if (offIdColl.Split(',').Contains(OfferID.ToString()))
    
    0 讨论(0)
  • 2021-01-03 01:02

    Something like this?

    string input = "r,v,l,m";
    string output = String.Join(",", input.Split(',').Where(YourLogic));
    
    bool YourLogic(string x)
    {
        return true;
    }
    
    0 讨论(0)
  • 2021-01-03 01:05

    // If you want to remove ALL occurences of the item, say "a" you can use

      String data = "a, b, c, d, a, e, f, q, a";
    
      StringBuilder Sb = new StringBuilder();
    
      foreach (String item in data.Split(',')) {
        if (!item.Trim().Equals("a", StringComparison.Ordinal)) {
          if (Sb.Length > 0) 
            Sb.Append(',');
    
          Sb.Append(item);
        }
      }
    
      data = Sb.ToString();
    
    0 讨论(0)
  • 2021-01-03 01:09

    Not going about this right. Do you need to keep the string? I doubt you do. Just use a list instead. Can you have duplicates? If not:

    offIdColl = my_Order.CustomOfferAppliedonOrder.TrimEnd(',').Split(',');
    
    if (offIdColl.Contains(OfferID.ToString()))
    {
        offIdColl.Remove(OfferID.ToString());
    }
    
    0 讨论(0)
提交回复
热议问题