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

                    }

                }
  • 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. – Jon Skeet Jun 08 '20 at 07: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` - is that possible? (you *can* remove from lists) – Marc Gravell Jun 08 '20 at 07:49
  • Reopened as this isn't about removing the numeric parts *of* a string. – Jon Skeet Jun 08 '20 at 07:49
  • @JonSkeet Yeah, you're probably correct. – Zohar Peled Jun 08 '20 at 07:51
  • related, perhaps a good dupe target https://stackoverflow.com/questions/17349179/how-can-i-remove-numbers-digits-from-strings-in-a-liststring – Drag and Drop Jun 08 '20 at 08:17
  • 1
    Does this answer your question? [How can I remove numbers/digits from strings in a List?](https://stackoverflow.com/questions/17349179/how-can-i-remove-numbers-digits-from-strings-in-a-liststring) – Drag and Drop Jun 08 '20 at 08:18

3 Answers3

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
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
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.

devcrp
  • 1,323
  • 7
  • 17
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

  • Your alternative (without lambda) would throw an exception due to modifying the collection while it was being enumerated. – pinkfloydx33 Jun 08 '20 at 08:48