0

I'd like to filter my array and delete only strings. I've tried few things but nothing works. Could anyone help me with this? What is wrong with my code?

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class Program
    {
        public static int[] FilterArray(object[] arr)
        {
            ArrayList myAL = new ArrayList(arr);
                        foreach (var ar in myAL) {
                        if (ar is string) {
                            myAL.Remove(ar);
                        }
                }
                    int[] myArray = myAL.Cast<int>().ToArray();
                    return myArray;
        }
    }

Best regards

7
  • Do you want to remove strings or return ints as this function seems to suggest? Commented Nov 29, 2019 at 14:21
  • Try : if (ar.GetType() == typeof(string)) { Commented Nov 29, 2019 at 14:21
  • 1
    What do you mean by nothing works? Are you getting any error? What sample input you are using and what output you are getting? Commented Nov 29, 2019 at 14:21
  • ar.GetType() != typeof(string) Commented Nov 29, 2019 at 14:22
  • 1
    You can´t modify a list while iterating it. Commented Nov 29, 2019 at 14:34

4 Answers 4

2

This way:

public static object[] RemoveStrings(object[] arr)
{
    return arr.Where(x => (x is string) == false).ToArray();
}

Using ArrayList is unnecessary


If you want to support all enumerables:

public static IEnumerable<object> RemoveStrings(IEnumerable<object> items)
{
    return items.Where(x => (x is string) == false);
}

Usage example:

class Program
{
    static void Main(string[] args)
    {
        List<object> items = new List<object>() {
            "pizza",
            1,
            4,
            10,
            "banana"
        };

        object[] filteredItems = RemoveStrings(items).ToArray();

        foreach(object item in filteredItems)
        {
            Console.WriteLine(item);
        }
    }

    public static IEnumerable<object> RemoveStrings(IEnumerable<object> items)
    {
        return items.Where(x => (x is string) == false);
    }
}

Output:

1
4
10
Sign up to request clarification or add additional context in comments.

Comments

1

You can't remove elements from the ArrayList while inside a foreach loop. Use a for loop instead. Or you could just do this

return arr.OfType<int>().ToArray(); //courtesy of @DavidG

Or if your goal was to simply remove the string elements from the array, it's much simpler using List<>'s .RemoveAll() method:

List<object> objects = new List<object>(arr);
objects.RemoveAll(x => x is string);

The modification will be applied to the list though, not the original array.

4 Comments

Much easier to do arr.OfType<int>().ToArray() though.
Care to explain why only taking ints is better than removing strings?
@DavidG that is cleaner indeed
Of course, it still doesn't deal with my comment on the question. And no need to credit me, but thank you anyway :)
0

Here is a quick suggestion for a solution:

using System;
using System.Linq;


namespace RemoveStringFromArray
{
    class Program
    {
        public static int[] RemoveString(object[] a)
        {
            return (from e in a
                    where !(e is string)
                    select (int) e).ToArray();
        }
        static void Main(string[] args)
        {
            object[] a = new object[5];
            a[0] = "Start";
            a[1] = 1;
            a[2] = "Midle";
            a[3] = 10;
            a[4] = "End";

            foreach (var e in a)
            {
                Console.WriteLine("Element {0}", e);
            }

            var i = RemoveString(a);

            Console.WriteLine("After removing strings");

            foreach (var e in i)
            {
                Console.WriteLine("Element {0}", e);
            }

        }
    }
}

This gives the output:

Element Start
Element 1
Element Midle
Element 10
Element End
After removing strings
Element 1
Element 10

Hope it helps.

Comments

0

You don't need to remove values from the array. Your method seems to return only integers from the original array. Any other type would throw if you use Cast<int>, even longs.

This will throw :

var arr=new object[]{1,4L};
var result=arr.Cast<int>();

The equivalent to your function is a simple call to OfType<> instead of Cast<>, eg :

var intsOnly=arr.OfType<int>().ToArray();

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.