簡體   English   中英

用分隔符分割String []並將其轉換為2D數組

[英]split String[] with seperators and convert it to 2D Array

我的c#代碼有問題。

我有一個字符串數組,每行包含23個值,以分號分隔。 我想分割每個值並將其解析為2D [double]數組,該數組應如下所示:

[(行數),22]。

字符串數組如下所示:

[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

雙精度數組應如下所示:

[0,0] 0,00

[0,1] 111,00

[0,2] 0,00

[0,3] -1,00

等等。

你有什么想法?

這是我當前的代碼,不起作用。

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++;                                                
    }
}

如何實現這一目標:

我在這里使用十進制,如果您使用double,它將從類似0,00的字符串中得到結果0 因此,您可以使用double,但如果可以,它將縮短它。 Whit十進制字符串,如0,00將為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]);
            }
        }

請嘗試此代碼。

        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++;
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM