troubleshooting Question

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

Avatar of Skale
Skale asked on
C#
11 Comments1 Solution102 ViewsLast Modified:
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 };

        }
ASKER CERTIFIED SOLUTION
it_saige
Developer
Join our community to see this answer!
Unlock 1 Answer and 11 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 11 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros