I have this String: "player.login name=username;x=52;y=406" how would I be able to split it so I easily could do Player pl = new Player(name, x, y) ?
I tried with a regex that looks like this: "([a-zA-Z_]+)[=]{1}([a-zA-Z0-9_]+)[;]{1}" but I’m not very good at regexs so it didn’t work.
EDIT: Someone came up with a good solution so no need to comment. 🙂
What I used:
public static void main(String args[]) {
String login = "player.login name=username;x=52;y=406";
String str = login.substring("player.login".length() + 1);
String[] sp = str.split(";");
Player player = new Player("", 0, 0);
for (String s : sp) {
String[] a = s.split("=");
if (a[0].equals("name")) player.username = a[1];
else if (a[0].equals("x")) player.x = toInt(a[1]);
else if (a[0].equals("y")) player.y = toInt(a[1]);
}
System.out.println("Player: " + player.username + " @ " + player.x + ", " + player.y);
}
public static int toInt(String s) {
return Integer.parseInt(s);
}
This should work (you should add bound checks before calling
exp.split("=")[1]):