-1

这个问题是如何删除 toString() 中的最后一个分隔符的延续。

我需要解析的字符串是:

719.0|501.0|-75.0,501.0|508.0|-62.0,-75.0|-62.0|10.0#-19.0|-19.0|-19.0|-19.0|-19.0|-19.0|-19.0|-19.0|-19.0|-19.0,-20.0|-20.0|-20.0|-20.0|-20.0|-20.0|-20.0|-20.0|-20.0|-20.0,0.0|0.0|0.0|0.0|0.0|0.0|0.0|0.0|0.0|0.0

如何将它们放入 2 个字符串数组中?

String [][] key = 719.0 501.0   -75.0   
                  501.0 508.0   -62.0   
                  -75.0 -62.0   10.0

String [][] value = -19.0   -19.0   -19.0   -19.0   -19.0   -19.0   -19.0   -19.0   -19.0   -19.0   
                    -20.0   -20.0   -20.0   -20.0   -20.0   -20.0   -20.0   -20.0   -20.0   -20.0   
                     0.0    0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0

问题是,我可能不知道行数和列数。但可以肯定的是,只有 2 个这样的矩阵。

有任何想法吗?

4

2 回答 2

1

如何在“#”处拆分字符串并分别解析两个结果字符串?

于 2013-11-05T08:40:27.277 回答
1

试试看。但在这里我假设您的行具有相同的数字元素。所以不可能有 5 列的一行和 3 列的另一行。

public class StringToArraySandBox {

        protected void doShow(String text) {

            for (String array : text.split("#")) {
                String[][] array1 = doParsing(array);

                for (int i = 0; i < array1.length; i++) {
                    for (int y = 0; y < array1[i].length; y++) {
                        System.out.print(array1[i][y]);
                        System.out.print(" ");
                    }
                    System.out.println();
                }

            }
        }

        protected String[][] doParsing(String text) {

            String[][] result = null;

            String[] rows = text.split(",");
            int rowIndex = 0;
            for (String row : rows) {
                String[] columns = row.split("\\|");

                if (result == null)
                    result = new String[rows.length][columns.length];

                int columnIndex = 0;
                for (String column : columns) {
                    result[rowIndex][columnIndex] = column;

                    columnIndex++;
                }

                rowIndex++;
            }

            return result;
        }

        public static void main(String[] args) {
            String target = "719.0|501.0|-75.0,501.0|508.0|-62.0,-75.0|-62.0|10.0#-19.0|-19.0|-19.0|-19.0|-19.0|-19.0|-19.0|-19.0|-19.0|-19.0,-20.0|-20.0|-20.0|-20.0|-20.0|-20.0|-20.0|-20.0|-20.0|-20.0,0.0|0.0|0.0|0.0|0.0|0.0|0.0|0.0|0.0|0.0";
            new StringToArraySandBox().doShow(target);
        }

    }
于 2013-11-05T09:26:28.700 回答