Link to home
Start Free TrialLog in
Avatar of Skale
Skale

asked on

What's the logic way to parse string, and return as int array for my case in C#

Hello,

I'm using below method to return int[] array for my index list but i assume there should be more easy way to adopt it.

Any help would be greatful. Thank you.

 public static int[] ReturnIndexes()
        {

            // Default values should be -1
            int index1 = -1;
            int index2 = -1;
            int index3 = -1;
            int index4 = -1;
            int index5 = -1;
            int index6 = -1;
            int index7 = -1;
            int index8 = -1;
            int index9 = -1;

            string param = "#1#1#2";
            //Sample param values
            //#1#1#1#2#3#4
            //#1
            //#2#1
            //#3#3#1


            string[] index = param.Split('#').Where(x => !string.IsNullOrWhiteSpace(x)).ToArray();

            switch (index.Length)
            {
                case 1:
                    index1 = Convert.ToInt32(index[0]) - 1;
                    break;
                case 2:
                    index1 = Convert.ToInt32(index[0]) - 1;
                    index2 = Convert.ToInt32(index[1]) - 1;
                    break;
                case 3:
                    index1 = Convert.ToInt32(index[0]) - 1;
                    index2 = Convert.ToInt32(index[1]) - 1;
                    index3 = Convert.ToInt32(index[2]) - 1;
                    break;
                case 4:
                    index1 = Convert.ToInt32(index[0]) - 1;
                    index2 = Convert.ToInt32(index[1]) - 1;
                    index3 = Convert.ToInt32(index[2]) - 1;
                    index4 = Convert.ToInt32(index[3]) - 1;
                    break;
                case 5:
                    index1 = Convert.ToInt32(index[0]) - 1;
                    index2 = Convert.ToInt32(index[1]) - 1;
                    index3 = Convert.ToInt32(index[2]) - 1;
                    index4 = Convert.ToInt32(index[3]) - 1;
                    index5 = Convert.ToInt32(index[4]) - 1;
                    break;
                case 6:
                    index1 = Convert.ToInt32(index[0]) - 1;
                    index2 = Convert.ToInt32(index[1]) - 1;
                    index3 = Convert.ToInt32(index[2]) - 1;
                    index4 = Convert.ToInt32(index[3]) - 1;
                    index5 = Convert.ToInt32(index[4]) - 1;
                    index6 = Convert.ToInt32(index[5]) - 1;
                    break;
                case 7:
                    index1 = Convert.ToInt32(index[0]) - 1;
                    index2 = Convert.ToInt32(index[1]) - 1;
                    index3 = Convert.ToInt32(index[2]) - 1;
                    index4 = Convert.ToInt32(index[3]) - 1;
                    index5 = Convert.ToInt32(index[4]) - 1;
                    index6 = Convert.ToInt32(index[5]) - 1;
                    index7 = Convert.ToInt32(index[6]) - 1;
                    break;
                case 8:
                    index1 = Convert.ToInt32(index[0]) - 1;
                    index2 = Convert.ToInt32(index[1]) - 1;
                    index3 = Convert.ToInt32(index[2]) - 1;
                    index4 = Convert.ToInt32(index[3]) - 1;
                    index5 = Convert.ToInt32(index[4]) - 1;
                    index6 = Convert.ToInt32(index[5]) - 1;
                    index7 = Convert.ToInt32(index[6]) - 1;
                    index8 = Convert.ToInt32(index[7]) - 1;
                    break;
                case 9:
                    index1 = Convert.ToInt32(index[0]) - 1;
                    index2 = Convert.ToInt32(index[1]) - 1;
                    index3 = Convert.ToInt32(index[2]) - 1;
                    index4 = Convert.ToInt32(index[3]) - 1;
                    index5 = Convert.ToInt32(index[4]) - 1;
                    index6 = Convert.ToInt32(index[5]) - 1;
                    index7 = Convert.ToInt32(index[6]) - 1;
                    index8 = Convert.ToInt32(index[7]) - 1;
                    index9 = Convert.ToInt32(index[8]) - 1;
                    break;
            }


            return new int[]{ index1, index2, index3, index4, index5, index6, index7, index8, index9 };

        }

Open in new window

Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

I think you are doing too much there.
Forget the index1, index2...  Create an array with nine items.  Then use a loop 0 to 8, whilst going through the loop you either assign the value like you do eg. Convert.ToInt32(index[0]) - 1 or if the loop counter is more than the number of items in index then assign your default -1.  Then return this array from your function.

have you tried something like this:

        public static int[] ReturnIndexes()
        {
            string param = "#1#1#2";
            List<int> integers = (param.Split('#').Where(x => !string.IsNullOrWhiteSpace(x)).ToArray()).Select(int.Parse).ToList();
            while (integers.Count() < 9)
            {
                integers.Add(-1);
            }
            return integers.ToArray();
        }

Open in new window

I don't know why the previous code snippet removed the type of my list!


The declaration should be:

List<int> integers = ...

The compact LINQ version should be:

public static int[] ReturnIndexes2(string param)
{
    return param.Split('#')
           .Where(s => !string.IsNullOrWhiteSpace(s))
           .Select(s => Convert.ToInt32(s) - 1)
           .Concat(Enumerable.Range(0, 8).Select(n => -1).ToArray())
           .Take(9)
           .ToArray();
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
A simple alternative:



int[] intIndex  = Array.ConvertAll<string, Int32>(index,
(x) => Convert.ToInt32(x));

Open in new window


That's All!

@Juan - the result must be an array with a fixed number of items, the 'extra' ones not in the string are given a default value.  Your code doesn't work as requested.

You can parse the array elements:


string param = "#1#1#2";

const int elements = 9;
const string defaultValue = "0";
char[] separator = { '#' };

string defaultValues = string.Concat(Enumerable.Repeat(separator[0] + defaultValue, elements));

string input = param + defaultValues;
string[] values = input.Split(separator, elements + 1, StringSplitOptions.RemoveEmptyEntries);
int[] index = values.Select(s => int.Parse(s) - 1).Take(elements).ToArray();

Open in new window

@Gustav: you're off by one..

namespace ConsoleCS
{
    using System;
    using System.Linq;

    public class Program
    {
        public static void Main(string[] args)
        {
            ReturnIndexes1("#1#1#2").ToConsole<int>("1--1", ';');
            ReturnIndexes1("#1#1#1#2#3#4").ToConsole<int>("1--2", ';');
            ReturnIndexes1("#3#3#1").ToConsole<int>("1--5", ';');
            ReturnIndexes1("#3# #1").ToConsole<int>("1--6", ';');

            ReturnIndexes2("#1#1#2").ToConsole<int>("2--1", ';');
            ReturnIndexes2("#1#1#1#2#3#4").ToConsole<int>("2--2", ';');
            ReturnIndexes2("#3#3#1").ToConsole<int>("2--5", ';');
            ReturnIndexes2("#3# #1").ToConsole<int>("2--6", ';');

            ReturnIndexes3("#1#1#2").ToConsole<int>("3--1", ';');
            ReturnIndexes3("#1#1#1#2#3#4").ToConsole<int>("3--2", ';');
            ReturnIndexes3("#3#3#1").ToConsole<int>("3--5", ';');
            ReturnIndexes3("#3# #1").ToConsole<int>("3--6", ';');

            Console.WriteLine("\nDone.");
            Console.ReadLine();
        }

        public static int[] ReturnIndexes3(string param)
        {
            const int elements = 9;
            const string defaultValue = "-1";
            char[] separator = { '#' };
            string defaultValues = string.Concat(Enumerable.Repeat(separator[0] + defaultValue, elements));
            string input = param + defaultValues;
            string[] values = input.Split(separator, elements + 1, StringSplitOptions.RemoveEmptyEntries);
            return values.Select(int.Parse).Take(elements).ToArray();
        }

        public static int[] ReturnIndexes2(string param)
        {
            return param.Split('#')
                .Where(s => !string.IsNullOrWhiteSpace(s))
                .Select(s => int.Parse(s) - 1)
                .Concat(Enumerable.Range(0, 8).Select(n => -1).ToArray())
                .Take(9)
                .ToArray();
        }

        public static int[] ReturnIndexes1(string param)
        {
            int index1 = -1;
            int index2 = -1;
            int index3 = -1;
            int index4 = -1;
            int index5 = -1;
            int index6 = -1;
            int index7 = -1;
            int index8 = -1;
            int index9 = -1;
            string[] index = param.Split('#').Where(x => !string.IsNullOrWhiteSpace(x)).ToArray();
            switch (index.Length)
            {
                case 1:
                    index1 = Convert.ToInt32(index[0]) - 1;
                    break;
                case 2:
                    index1 = Convert.ToInt32(index[0]) - 1;
                    index2 = Convert.ToInt32(index[1]) - 1;
                    break;
                case 3:
                    index1 = Convert.ToInt32(index[0]) - 1;
                    index2 = Convert.ToInt32(index[1]) - 1;
                    index3 = Convert.ToInt32(index[2]) - 1;
                    break;
                case 4:
                    index1 = Convert.ToInt32(index[0]) - 1;
                    index2 = Convert.ToInt32(index[1]) - 1;
                    index3 = Convert.ToInt32(index[2]) - 1;
                    index4 = Convert.ToInt32(index[3]) - 1;
                    break;
                case 5:
                    index1 = Convert.ToInt32(index[0]) - 1;
                    index2 = Convert.ToInt32(index[1]) - 1;
                    index3 = Convert.ToInt32(index[2]) - 1;
                    index4 = Convert.ToInt32(index[3]) - 1;
                    index5 = Convert.ToInt32(index[4]) - 1;
                    break;
                case 6:
                    index1 = Convert.ToInt32(index[0]) - 1;
                    index2 = Convert.ToInt32(index[1]) - 1;
                    index3 = Convert.ToInt32(index[2]) - 1;
                    index4 = Convert.ToInt32(index[3]) - 1;
                    index5 = Convert.ToInt32(index[4]) - 1;
                    index6 = Convert.ToInt32(index[5]) - 1;
                    break;
                case 7:
                    index1 = Convert.ToInt32(index[0]) - 1;
                    index2 = Convert.ToInt32(index[1]) - 1;
                    index3 = Convert.ToInt32(index[2]) - 1;
                    index4 = Convert.ToInt32(index[3]) - 1;
                    index5 = Convert.ToInt32(index[4]) - 1;
                    index6 = Convert.ToInt32(index[5]) - 1;
                    index7 = Convert.ToInt32(index[6]) - 1;
                    break;
                case 8:
                    index1 = Convert.ToInt32(index[0]) - 1;
                    index2 = Convert.ToInt32(index[1]) - 1;
                    index3 = Convert.ToInt32(index[2]) - 1;
                    index4 = Convert.ToInt32(index[3]) - 1;
                    index5 = Convert.ToInt32(index[4]) - 1;
                    index6 = Convert.ToInt32(index[5]) - 1;
                    index7 = Convert.ToInt32(index[6]) - 1;
                    index8 = Convert.ToInt32(index[7]) - 1;
                    break;
                case 9:
                    index1 = Convert.ToInt32(index[0]) - 1;
                    index2 = Convert.ToInt32(index[1]) - 1;
                    index3 = Convert.ToInt32(index[2]) - 1;
                    index4 = Convert.ToInt32(index[3]) - 1;
                    index5 = Convert.ToInt32(index[4]) - 1;
                    index6 = Convert.ToInt32(index[5]) - 1;
                    index7 = Convert.ToInt32(index[6]) - 1;
                    index8 = Convert.ToInt32(index[7]) - 1;
                    index9 = Convert.ToInt32(index[8]) - 1;
                    break;
            }

            return new int[] { index1, index2, index3, index4, index5, index6, index7, index8, index9 };
        }
    }
}

Open in new window

with

namespace ConsoleCS
{
    using System;
    using System.Collections.Generic;

    public static class EnumerableHelper
    {
        public static void ToConsole<T>(this IEnumerable<T> enumerable)
        {
            foreach (T item in enumerable)
            {
                Console.WriteLine(item);
            }
        }

        public static void ToConsole<T>(this IEnumerable<T> enumerable, string caption)
        {
            Console.WriteLine(caption);
            foreach (T item in enumerable)
            {
                Console.WriteLine(item);
            }

            Console.WriteLine("");
        }

        public static void ToConsole<T>(this IEnumerable<T> enumerable, string caption, char separator)
        {
            Console.WriteLine(caption);
            foreach (T item in enumerable)
            {
                Console.Write(item);
                Console.Write(separator + " ");
            }

            Console.WriteLine("");
            Console.WriteLine("");
        }

        public static void ToConsole<T>(this IEnumerable<T> enumerable, Func<T, string> toString)
        {
            foreach (T item in enumerable)
            {
                Console.WriteLine(toString(item));
            }
        }

        public static void ToConsole<T>(this IEnumerable<T> enumerable, string caption, Func<T, string> toString)
        {
            Console.WriteLine(caption);
            foreach (T item in enumerable)
            {
                Console.WriteLine(toString(item));
            }

            Console.WriteLine("");
        }
    }
}

Open in new window

I've used his code, it's the output starting with "1--n":

User generated image
In my code its line 44: Select(s => int.Parse(s) - 1)

vs.

your line11: .Select(int.Parse).

That's what his -1 are doing.

Oh, missed that, thanks!

Corrected the default value and subtracted 1.