分裂成两个单独的字符串

时间:2018-03-03 11:49:20

标签: java

我的意思是,我用类型String创建变量然后当我加载文件时,我必须将内容保存在一个变量中,在这种情况下变量是“TextFromFile”,加载后,我想将它拆分分成两个单独的并保存在数组中,之后我只想在屏幕上显示两个单独的字符串。这就是想法。

String[] pole = TextFromFile.split("[ \\ ]");
        System.out.println("Frist matrica " + pole[0] +" Second matrica: " + pole[1] +"");

只有这部分显示我的阵列没有弹跳:

{{1}}

错误:

  

线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:1         在zadania1.Zadania1.main(Zadania1.java:24)

1 个答案:

答案 0 :(得分:0)

根据我从评论中理解的内容,假设您的输入采用以下形式:

[1,2,3;4,5,6;][9,8;7,-6;50,61;]

你需要在分割中使用另一个正则表达式,这涉及一个后视:

String input = "[1,2,3;4,5,6;][9,8;7,-6;50,61;]";
String[] pole = input.split("(?<=\\])");
System.out.println("Frist matrica " + pole[0] +" Second matrica: " + pole[1] +"");

// Output:
// Frist matrica [1,2,3;4,5,6;] Second matrica: [9,8;7,-6;50,61;]