我有一个看起来像这样的字符串

String read = "1130:5813|1293:5803|1300:5755|1187:5731|"


如您所见,有4对整数值。

我想在列表中添加类似这样的值

a = 1130
b = 5813

groupIt pair = new groupIt(a,b);
List<groupIt> group  = new ArrayList<groupIt>();
group.add(pair);


我该如何对4对String进行此操作。

可以使用Pattern.compile()吗?

最佳答案

你为什么不使用

String[] tokens = read.split("\\|");
for (String token : tokens) {
   String[] params = token.split(":");
   Integer a = Integer.parseInt(params[0]);
   Integer b = Integer.parseInt(params[1]);

   // ...

}

10-04 22:43
查看更多