Share via


Convert and Split an array of strings into integers

Question

Friday, January 25, 2019 11:00 AM

Hello

I have to select my data from a datatable and then to convert them into an array of integer

this is what I have so far

 var _values = (from row in dt.AsEnumerable()
                                    select row.Field<string>(0).Split(' ')).ToArray();

and I convert like this

int[] icnums = _values.Split(' ').Select(int.Parse).ToArray();

But I get the error 

'string[][]' does not contain a definition for 'Split' and no extension method 'Split' accepting a first argument of type 'string[][]' could be found (are you missing a using directive or an assembly reference?) 

I have also try this but no luck

Any idea thank you!

All replies (11)

Friday, January 25, 2019 11:51 AM

Is not clear if your row is like "45 test 32", "45 test", "45" or "test"
In any case you should test for numeric value. Not sure if in C#, IsNumeric can be translated to Information.IsNumeric

Dim lst As New List(Of String)
lst.Add("23")
lst.Add("13")
lst.Add("5")
lst.Add("X")

Dim numbers = From p In lst Where IsNumeric(p) Select Integer.Parse(p) ' result is 23, 13, 5

Friday, January 25, 2019 12:52 PM

Well

My rows are a strings of kind

"1 2 3 4 5"

"1 3 5 4 6"

etc

thank you  


Friday, January 25, 2019 2:04 PM

Try

string nums = "1 2 3 4 5";
var numArray = nums.Split(' ').Select(int.Parse).ToArray();

Sunday, January 27, 2019 9:57 PM

I think you want SelectMany - like this (a single statement, you can split it into two statements if you require it)

var icnums = dt.AsEnumerable()
               .SelectMany(row => row.Field<string>(0).Split(' ')
                                     .Select(s => Int.Parse(s));

You can append .ToArray() if you absolutely, positively, 100% are forced to have an array. (My guess is that you don't really need the array)

(Sorry, I only use the fluent syntax, you'll have to work out the query syntax version yourself if you are forced to write in query syntax)

As already suggested, if there is any possibility that the data is not valid then you should test with Int.TryParse and take appropriate defensive action.


Monday, January 28, 2019 2:40 AM

Hi mspace,

'string[][]' does not contain a definition for 'Split' and no extension method 'Split' accepting a first argument of type 'string[][]' could be found (are you missing a using directive or an assembly reference?) 

I have also try this but no luck

It seems the _values was a Multi-dimensional Array(two Dimensional Array). You may need to try the following way to convert two Dimensional string Array to two Dimensional int Array.Finally traversing the two Dimensional int Array.

        protected void Button1_Click(object sender, EventArgs e)
        {
            string nums = "1 2 3 4 5";
            var numArray = nums.Split(' ').Select(int.Parse).ToArray();

            string[,] array = new string[3,2];
            array[0, 0] = "1";
            array[0, 1] = "2";
            array[1, 0] = "3";
            array[1, 1] = "4";
            array[2, 0] = "5";
            array[2, 1] = "6";

            int[,] newintarray = ToIntArray(array);

            Response.Write("Length: "+ newintarray.Length + "</br>");
            Response.Write("Row: " + newintarray.GetLength(0) + "</br>");
            Response.Write("Col: " + newintarray.GetLength(1) + "</br>");

            for (int i = 0; i < newintarray.GetLength(0); i++)
            {
                for (int j = 0; j < newintarray.GetLength(1); j++)
                {
                    Response.Write(newintarray[i,j] + "</br>");
                    //Console.WriteLine(array[i, j]);
                }
            }
        }


        public int[,] ToIntArray(string[,] twoDimensionalArray)
        {
            int rowsFirstIndex = twoDimensionalArray.GetLowerBound(0);
            int rowsLastIndex = twoDimensionalArray.GetUpperBound(0);
            int numberOfRows = rowsLastIndex + 1;

            int columnsFirstIndex = twoDimensionalArray.GetLowerBound(1);
            int columnsLastIndex = twoDimensionalArray.GetUpperBound(1);
            int numberOfColumns = columnsLastIndex + 1;

            int[,] newintArray = new int[numberOfRows, numberOfColumns];
            for (int i = rowsFirstIndex; i <= rowsLastIndex; i++)
            {
                for (int j = columnsFirstIndex; j <= columnsLastIndex; j++)
                {
                    newintArray[i,j] = Convert.ToInt32(twoDimensionalArray[i, j]);
                }
            }
            return newintArray;
        }

Best Regards,

Yong Lu


Tuesday, January 29, 2019 5:52 AM

Hello Yohann Lu,

thank you for your reply! I understand your logic but my problem is on the query on my datatable....

I read my data from a datatable....and when I use

 string _values = dt.Rows[0].ItemArray.Select(x => x.ToString());

I get the error

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<string>' to 'string'  

thank you again...


Tuesday, January 29, 2019 6:10 AM

Hi mspace,

Hello Yohann Lu,

thank you for your reply! I understand your logic but my problem is on the query on my datatable....

I read my data from a datatable....and when I use

 string _values = dt.Rows[0].ItemArray.Select(x => x.ToString());

I get the error

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<string>' to 'string'   

thank you again...

You can try the following code.

            IEnumerable<string> _values = dt.Rows[0].ItemArray.Select(x => x.ToString());

            Or
            List<string> _valuesT = dt.Rows[0].ItemArray.Select(x => x.ToString()).ToList();

Best Regards,

Yong Lu


Tuesday, January 29, 2019 6:21 AM

Hello PaulTheSmith,

When I try

var icnums = colscobs.AsEnumerable()
.SelectMany(row => row.Field<string>(0).Split(' ')
.Select(s => int.Parse(s))).ToArray();

I get all rows like that

Which is the logic...I need each row to be single...

thank you


Tuesday, January 29, 2019 6:53 AM

Can you give a sample of the input data and what result you require?  Make it sufficiently complex to represent all the business rules you are applying.


Tuesday, January 29, 2019 7:01 AM

well I generate my input data through this

https://www.technical-recipes.com/2017/obtaining-combinations-of-k-elements-from-n-in-c/#comment-52343

thank you


Tuesday, January 29, 2019 11:44 AM

Can you give a sample of your data with the corresponding desired output, please?