Click here to Skip to main content
15,359,134 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
string Batches = ",,H104001002,H104001003,H104001004,H104001005";

I have this string and i have to remove "H104001002" from the above string in ASP.Net C#.

What I have tried:

I was thinking to use Contains to find the string and Replace to replace it with ""
Posted
Updated 9-Aug-16 0:01am
v2
Comments
Hein Pauwelyn 20-Jul-16 4:03am
   
Why shouldn't it work by replacing? Anyway, you could also try to use regex. https://msdn.microsoft.com/en-us/library/xwewhkd1(v=vs.110).aspx

It depends exactly what you are trying to do: removing an absolute value (one that never changes) is trivial:
C#
string Batches = ",,H104001002,H104001003,H104001004,H104001005";
string changed = Batched.Replace("H104001002", "");
will do it.
But...if string isn't "fixed" or can appear in other places in the data correctly, then that isn't a viable alternative. For example, usign that method on this:
C#
string Batches = ",,H104001002,H104001003,H104001002,H104001005"
Would give you:
",,,H104001003,,H104001005"

There are a number of ways to do it: you could split the string, kill the "bad" location, and rebuild it:
C#
string Batches = ",,H104001002,H104001003,H104001004,H104001005";
string[] parts = Batches.Split(',');
parts[2] = "";
string changed = string.Join(",", parts);

Or you could use a Regex:
C#
string Batches = ",,H104001002,H104001003,H104001004,H104001005";
string changed = Regex.Replace(Batches, @"(?<=\,\,).+?(?=,)", "");

But...I'd suggest that you look at using an existing CSV parser: A Fast CSV Reader[^] is a good choice.
   
Comments
Abrar Kazi 20-Jul-16 8:52am
   
Thanks :)
OriginalGriff 20-Jul-16 9:11am
   
You're welcome!
You could try to use regular expressions (or regex). In your case the code will be this:

C#
using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string input = ",,H104001002,H104001003,H104001004,H104001005"
      string pattern = "H104001002";
      string replacement = "";
      Regex rgx = new Regex(pattern);
      string result = rgx.Replace(input, replacement);

      Console.WriteLine("Original String: {0}", input);
      Console.WriteLine("Replacement String: {0}", result);                             
   }
}
   
Comments
Abrar Kazi 20-Jul-16 8:52am
   
Thanks :)
C#
// using simple Replace method.
     string Batches = ",,H104001002,H104001003,H104001002,H104001004,H104001005";
     string find = "H104001002";
     Batches = Batches.Replace(find, "");


     // using Array/List to replace it, this will remove the empty items
     List<string> lst = new List<string>();
     var array = Batches.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
     if(array.Contains(find))
         for (int i = 0; i < array.Length; i++)
         {
             if (array[i] != find)
                 lst.Add(array[i]);
         }
     Batches = string.Join(",", lst);

     // using Linq
    Batches= string.Join(",", Batches.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Where(k => k != find).ToArray());
   
Comments
Abrar Kazi 20-Jul-16 8:52am
   
Thanks :)
you can split by,

C#
string[] namesArray = "Tom,Scott,Bob".Split(',');
   
Comments
Maciej Los 9-Aug-16 10:44am
   
My vote of 1. Split method will return the array of strings. OP wants to remove specific element in from comma separeted string.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900