Hi,

In c#, i have a string array like
string text[] = value.split(',');

Assume text contains the values a, ,b,-,c,d, ,
it has 2 white spaces and 1 hyphen values
i want to remove the white spaces and hyphens and want to make it as a,b,c,d

i have tried this

for (int counttext = 0; counttext < text.Length; counttext++)
{
if (text[counttext] == "" || text[counttext] == "-")
{
text[counttext].Remove(counttext,0);
}
}

but for final value i got error,.. in remove function
can anyone help me?

Edited 6 Years Ago by __avd: Added [code] tags. For easy readability, always wrap programming code within posts in [code] (code blocks).

I was able to get something like this to work:

string value = "a, ,b,-,c,d";
string[] text = value.Split(',');
//change it to a list
List<string> texlist = text.ToList();
texlist.RemoveAt(i);  //use a loop and the RemoveAt() method

I couldn't find a way to do it on the array directly but there may be a means to do it.

This should do the trick

// Split the values by comma
String[] values = "a,b,c,d,-,,e,f".Split(',');

// Create a new list to hold filtered content
List<String> filtered = new List<String>();

// Create regex to check each value
Regex reg = new Regex(@"^[a-zA-Z]+$", RegexOptions.IgnoreCase);

// Loop through values
foreach(String value in values)
{
    // Check if value is valid
    if(reg.Match(value))
    {
        // If value is valid then add to list
        filtered.Add(value);
    }
}

// Convert list back to values array if you need it in an array
values = filtered.ToArray();

Consider this method:

string[] excludedValues = { string.Empty, "-", " " };
string value = "a, ,b,-,c,d, ,";
string[] values = value.Split(',').Except(excludedValues).ToArray();

Hi,

In c#, i have a string array like
string text[] = value.split(',');

Assume text contains the values a, ,b,-,c,d, ,
it has 2 white spaces and 1 hyphen values
i want to remove the white spaces and hyphens and want to make it as a,b,c,d

i have tried this

for (int counttext = 0; counttext < text.Length; counttext++)
{
if (text[counttext] == "" || text[counttext] == "-")
{
text[counttext].Remove(counttext,0);
}
}

but for final value i got error,.. in remove function
can anyone help me?

U need to use stringbuilder class

StringBuilder st = new StringBuilder();
            string str = "abc_ d"; char n;
            for (int counttext = 0; counttext < str.Length; counttext++)
            {

                n = Convert.ToChar(str[counttext]);
                if (n != '_')
                {
                    if (n != ' ')
                    {
                        st.Append(n);
                    }
                }


            }



            Console.WriteLine(st);
            Console.ReadKey();



        }
    }
}

Edited 6 Years Ago by __avd: Added [code] tags. For easy readability, always wrap programming code within posts in [code] (code blocks).

static void Main(string[] args)
      {
         string strValue = "a, ,b,-,c,d, ,";
         
         string[] arr_strData =
            strValue.Split(", -".ToCharArray(), 
               StringSplitOptions.RemoveEmptyEntries);

         Console.WriteLine(string.Join(",", arr_strData));
      }

Edited 6 Years Ago by __avd: Added [code] tags. For easy readability, always wrap programming code within posts in [code] (code blocks).

This article has been dead for over six months. Start a new discussion instead.