We help IT Professionals succeed at work.

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

Hakan
Hakan asked
on
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

AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Commented:
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.
Éric MoreauSenior .Net Consultant
CERTIFIED EXPERT
Top Expert 2016

Commented:

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();
        }
Éric MoreauSenior .Net Consultant
CERTIFIED EXPERT
Top Expert 2016

Commented:

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


The declaration should be:

List<int> integers = ...

ste5anSenior Developer
CERTIFIED EXPERT

Commented:
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

Developer
CERTIFIED EXPERT
Distinguished Expert 2019
Commented:
You could also use Char.IsDigit; e.g. -
using System;
using System.Linq;

namespace EE_Q29170422
{
    class Program
    {
        static void Main(string[] args)
        {
            var indicies = new[] { "#1#1#2", "#1#1#1#2#3#4", "#1", "#2#1", "#3#3#1", "#=#.#$#1#2#3#4#5#6#7#8#9" };
            foreach (var index in indicies)
            {
                Console.WriteLine(string.Join(":", index.GetIndicies()));
            }
            Console.ReadLine();
        }
    }

    static class Extensions
    {
        public static int[] GetIndicies(this string source)
        {
            return (from c in source.Split(new[] { '#' }, StringSplitOptions.RemoveEmptyEntries)
                    where char.IsDigit(c.ToCharArray()[0])
                    select int.Parse(c) - 1)
                    .Concat(from i in Enumerable.Range(0, 8) select -1).Take(9).ToArray();
        }
    }
}

Open in new window

Which produces the following output -Capture.PNG-saige-
CERTIFIED EXPERT

Commented:
A simple alternative:



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

Open in new window


That's All!
AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Commented:

@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.

CERTIFIED EXPERT
Most Valuable Expert 2015
Distinguished Expert 2018

Commented:

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();

ste5anSenior Developer
CERTIFIED EXPERT

Commented:
@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

ste5anSenior Developer
CERTIFIED EXPERT

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

Capture.PNG
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.
CERTIFIED EXPERT
Most Valuable Expert 2015
Distinguished Expert 2018

Commented:

Oh, missed that, thanks!

Corrected the default value and subtracted 1.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.