1

I have an array of type string which has numbers and characters such as string[] TagsSeperatedArray={"tag1","tag2","1234",""} i want to remove 1234 and "" from the array. How can I do this. Can someone please help me. I am using asp.net c#.

foreach(var item in TagsSeperatedArray)
                {
                    if()
                    {

                    }

                }
6
  • You can't remove elements from an array, because you can't change the size. You can set the element to null instead - would that be enough for you? I suspect you should look at Int32.TryParse if what you're stuck on is detecting numeric strings. Commented Jun 8, 2020 at 7:49
  • 2
    firstly, note that you can't "remove" anything from an array - an array is (by definition) fixed size; you can replace values, or you can create a new array with fewer values; this would be easier with a List<string> - is that possible? (you can remove from lists) Commented Jun 8, 2020 at 7:49
  • Reopened as this isn't about removing the numeric parts of a string. Commented Jun 8, 2020 at 7:49
  • @JonSkeet Yeah, you're probably correct. Commented Jun 8, 2020 at 7:51
  • 1
    Does this answer your question? How can I remove numbers/digits from strings in a List<string>? Commented Jun 8, 2020 at 8:18

3 Answers 3

2

One way to do this would be:

var arr = new[] { "tag1", "tag2", "1234", "" };
Console.WriteLine(string.Join(",", arr)); // tag1,tag2,1234,
var newArr = arr.Where(value => !string.IsNullOrWhiteSpace(value)
    && !int.TryParse(value, out _)).ToArray();
Console.WriteLine(string.Join(",", newArr)); // tag1,tag2

Note, however, that this allocates an extra array etc; it would be more efficient with a list, since you can directly remove:

var list = new List<string> { "tag1", "tag2", "1234", "" };
Console.WriteLine(string.Join(",", list)); // tag1,tag2,1234,
list.RemoveAll(value => string.IsNullOrWhiteSpace(value) || int.TryParse(value, out _));
Console.WriteLine(string.Join(",", list)); // tag1,tag2
Sign up to request clarification or add additional context in comments.

Comments

0

You could use Linq for this, something like:

TagsSeparatedArray.Where(item => !int.TryParse(item, out int _))

This would exclude those that can be converted to int.

Comments

0

Based on the answer of @Marc Grawell

    private string[] RemoveNumerics(string[] TheArray)
    {
        List<string> list = new List<string>();
        list.AddRange(TheArray);
        list.RemoveAll(value => string.IsNullOrWhiteSpace(value) || int.TryParse(value, out _));
        return list.ToArray();
    }

And in case you do not want it as a lambda expression:

foreach(string value in list)
    if (string.IsNullOrWhiteSpace(value) || int.TryParse(value, out _))
        list.Remove(value);

Cheers

1 Comment

Your alternative (without lambda) would throw an exception due to modifying the collection while it was being enumerated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.