You could try:
String[] points = coord.split("\\s*[,]\\s*");
Where the split()
method returns an array of Strings.
Explanation -
\\s* -> Indicates zero-or-more spaces
[,] -> Indicates a comma (which is basically that one present between the points)
\\s* -> Indicates that there may be zero-or-more spaces between the comma and the next point
However, if your text may contain some words, then you would be better off using the Pattern and Matcher class by importing the java.util.regex
package:
String coords = "Hi there! 2.3 12.6786, -1234 7.3, 34 35, are the points!";
Pattern p = Pattern.compile("[0-9\\-][0-9 .\\-]+(?=,)");
Matcher m = p.matcher(coords);
while(m.find())
System.out.println("Point found = '" + m.group(0) + "'");
Which gives the output of:
Point found = '2.3 12.6786'
Point found = '-1234 7.3'
Point found = '34 35'
Where m.group(0)
contains the complete match (point) found.
Note - The above method works perfectly only if the last point's coordinates are followed by a comma, otherwise it will match the points upto, but not including, the last one.