分割字符串得到 coordinates/points
Split string to get coordinates/points
我正在使用以下代码拆分 (<x-coordinate>,<y-coordinate>)
:
String startPointStr = InputPattern.parsePoint(args.get(0));
String[] startCoords = startPointStr.substring(1, startPointStr.length() - 1).split(",");
Point startPoint = new Point(Integer.parseInt(startCoords[0]), Integer.parseInt(startCoords[1]));
现在,我想修改代码,使其也适用于 (<x-coordinate>,<y-coordinate>),(<x-coordinate>,<y-coordinate>)
。
最后我需要两个点,每个点有两个值。
如何做到这一点?
仔细查看了一下,一个有效的解决方案就像找到的代码示例 here 一样,它将忽略所有不是一系列数字的值。
Pattern p = Pattern.compile("\d+");
Matcher m = p.matcher("(100,200),(300,400)");
ArrayList<Integer> values = new ArrayList<>();
while(m.find())
values.add(Integer.parseInt(m.group()));
ArrayList<Point> points = new ArrayList<>();
if(values.size() % 2 == 0)
for(int i = 0; i < values.size(); i += 2)
points.add(new Point(values.get(i), values.get(i + 1)));
for(Point point : points)
System.out.println(point.x + ", " + point.y);
但是,如果您不担心效率问题,那么多重拆分解决方案将足以满足您的目的。
如果您使用 Java >= 9,您可以使用 Matcher 上的 results() 方法来获取 MatchResult 流并生成点对象列表:
String input = "(1,2),(3,4),(5,6),(500,600),(1000,2000)";
Pattern cp = Pattern.compile("(\d+(?=,),(?<=,)\d+)");
Matcher matcher = cp.matcher(input);
List<Point> points = matcher.results()
.map(coordinate -> {
String xy = coordinate.group();
int x = Integer.parseInt(xy.split(",")[0]);
int y = Integer.parseInt(xy.split(",")[1]);
return new Point(x, y);
})
.collect(Collectors.toList());
System.out.println("points: " + points);
结果:
points: [java.awt.Point[x=1,y=2], java.awt.Point[x=3,y=4], java.awt.Point[x=5,y=6], java.awt.Point[x=500,y=600], java.awt.Point[x=1000,y=2000]]
正则表达式模式获取不带括号的坐标。然后在 map 方法中使用 split 方法获取 x 和 y 坐标并生成 Point 对象。
我正在使用以下代码拆分 (<x-coordinate>,<y-coordinate>)
:
String startPointStr = InputPattern.parsePoint(args.get(0));
String[] startCoords = startPointStr.substring(1, startPointStr.length() - 1).split(",");
Point startPoint = new Point(Integer.parseInt(startCoords[0]), Integer.parseInt(startCoords[1]));
现在,我想修改代码,使其也适用于 (<x-coordinate>,<y-coordinate>),(<x-coordinate>,<y-coordinate>)
。
最后我需要两个点,每个点有两个值。
如何做到这一点?
仔细查看了一下,一个有效的解决方案就像找到的代码示例 here 一样,它将忽略所有不是一系列数字的值。
Pattern p = Pattern.compile("\d+");
Matcher m = p.matcher("(100,200),(300,400)");
ArrayList<Integer> values = new ArrayList<>();
while(m.find())
values.add(Integer.parseInt(m.group()));
ArrayList<Point> points = new ArrayList<>();
if(values.size() % 2 == 0)
for(int i = 0; i < values.size(); i += 2)
points.add(new Point(values.get(i), values.get(i + 1)));
for(Point point : points)
System.out.println(point.x + ", " + point.y);
但是,如果您不担心效率问题,那么多重拆分解决方案将足以满足您的目的。
如果您使用 Java >= 9,您可以使用 Matcher 上的 results() 方法来获取 MatchResult 流并生成点对象列表:
String input = "(1,2),(3,4),(5,6),(500,600),(1000,2000)";
Pattern cp = Pattern.compile("(\d+(?=,),(?<=,)\d+)");
Matcher matcher = cp.matcher(input);
List<Point> points = matcher.results()
.map(coordinate -> {
String xy = coordinate.group();
int x = Integer.parseInt(xy.split(",")[0]);
int y = Integer.parseInt(xy.split(",")[1]);
return new Point(x, y);
})
.collect(Collectors.toList());
System.out.println("points: " + points);
结果:
points: [java.awt.Point[x=1,y=2], java.awt.Point[x=3,y=4], java.awt.Point[x=5,y=6], java.awt.Point[x=500,y=600], java.awt.Point[x=1000,y=2000]]
正则表达式模式获取不带括号的坐标。然后在 map 方法中使用 split 方法获取 x 和 y 坐标并生成 Point 对象。