0

There is is a problem in my c# code.

I have a string array with 23 values in each line, seperated by a semicolon. I want to split each value and parse it into an 2D [double] Array that should look like:

[(number of lines),22].

The string array looks like:

[0] 0,00;111,00;0,00;-1,00;-1,00;0,00;0,00;0,00;0,00;0,00;0,00;0,00;0,00;0,00;0,10;-0,10;-1,00;-1,00;0,00;0,00;0,00;0,00;0,00

[1] 0,00;120,00;0,00;-1,00;-1,00;0,00;0,00;0,00;0,00;0,00;0,00;0,00;0,00;0,00;0,10;-0,10;-1,00;-1,00;0,00;0,00;0,00;0,00;0,00

The double array should look like:

[0,0] 0,00

[0,1] 111,00

[0,2] 0,00

[0,3] -1,00

and so on.

Do you have any ideas?

This is my current Code, that does not work.

double[,] values = new double[z, 22];
char[] seperator = { ';' };

int x = 0;

for (int i = 0; i < z; i++) {
    for (int j = 0; j < 22; j++) {
        values[i, j] = Data[x].Split(seperator);
        x++;                                                
    }
}
2
  • 1
    We have plenty of ideas. Like, "use a for loop". Or, "use String.Split( ';' )". But we are not a code writing service. So, start coding it, and come back here if it does not work. Commented May 14, 2017 at 12:51
  • It seems safe to assume that the , character is your decimal separator? Commented May 15, 2017 at 6:36

2 Answers 2

1

How you could achieve this:

I use decimal here if you use double it will get the result 0 from a string like 0,00. So you can use double but if it can it will shorten it. Whit decimal a string like 0,00 will be 0.00

        string[] arr = new string[] { "0,00;111,00;0,00;-1,00;-1,00;0,00;0,00;0,00;0,00;0,00;0,00;0,00;0,00;0,00;0,10;-0,10;-1,00;-1,00;0,00;0,00;0,00;0,00;0,00" , "0,00;120,00;0,00;-1,00;-1,00;0,00;0,00;0,00;0,00;0,00;0,00;0,00;0,00;0,00;0,10;-0,10;-1,00;-1,00;0,00;0,00;0,00;0,00;0,00" };
        // array conaining decimals
        decimal[,] numbers = new decimal[2,23];

        // loop thrue strings in arr
        for (int i = 0; i < arr.Length; i++)
        {
            // split that string by ';'
            string[] numberStrings = arr[i].Split(';');
            // loop thrue the result of the splitted strings
            for (int j = 0; j < numberStrings.Length; j++)
            {
                // parse that number from splitted string to decimal an put it on his place in the 2d array
                numbers[i, j] = decimal.Parse(numberStrings[j]);
            }
        }
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your Help! I'm new in programming, so I don't know how to deal with the multi array list. could I convert it into a double [,] array after the loop you wrote me?
Yes you can you can use decimal[][] finalArray = multiArray.ToArray(); You covert the list of decimal arrays to decimal 2d array.
First of all, Thanks a lot! I tried your code, but it didn't work fine for me. Is there a solution, if I just want to get an double [,] array as result and not a decimal [][].
I've changed my question hopefully this will for fill your needs. Notice that I use decimal instead of double. If you still want to use double just change all the decimal to double instead.
0

Please, try this code.

        String a1 = "0,00;111,00;0,00;-1,00;-1,00;0,00;0,00;0,00;0,00;0,00;0,00;0,00;0,00;0,00;0,10;-0,10;-1,00;-1,00;0,00;0,00;0,00;0,00;0,00";


        // new array split string by ';'
        String[] arr1 = a1.Split(';');

        int count = 0;


        Console.WriteLine("\n\n\n-----------\nType 1 (only 1 array) \n-----------\n\n\n");

        while ( count <= arr1.Length -1){
            Console.WriteLine(arr1[count].ToString());
            count++;
        }


        Console.WriteLine("\n\n\n-----------\nType 2 (multidimensional array) \n-----------\n\n\n");

        // new array split string by ';' and ','
        String[] arr2 = a1.Split(';');
        string[,] arrFinal = new string[23,2];


        // re-start counter
        count = 0;

        while (count <= arr2.Length - 1)
        {
            arrFinal[count, 0] = arr2[count].ToString().Split(',')[0];
            arrFinal[count, 1] = arr2[count].ToString().Split(',')[1];

            Console.WriteLine(arr2[count].ToString());
            Console.WriteLine("item ({0},{1}) = {2} | item ({3},{4}) = {5} ", count, "0", arrFinal[count, 0], count, "1", arrFinal[count, 1], arrFinal[count, 1] );
            count++;
        }

2 Comments

It might be useful to provide some context or explanation as to why this solves the problem.
i get the string lines from a .txt file wherefrom I seperate the values line by line. Afterwards I want to compare several values. Therefore I need the values in an array, so that i can access them quick.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.