We help IT Professionals succeed at work.
Get Started

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

Hakan
Hakan asked
on
96 Views
Last Modified: 2020-02-27
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

Comment
Watch Question
Developer
CERTIFIED EXPERT
Distinguished Expert 2019
Commented:
This problem has been solved!
Unlock 1 Answer and 11 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant

An Experts Exchange subscription includes unlimited access to online courses.

Get Started
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE