这个问题是How to remove the last separator in 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个这样的矩阵。
有什么想法吗?
答案 0 :(得分:1)
如何在'#'处拆分字符串并分别解析两个结果字符串?
答案 1 :(得分: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);
}
}