1

I have following URL: https://play.google.com/store/apps/details?id=eu.bandainamcoent.verylittlenightmares&hl=en";

I want to extract this value: eu.bandainamcoent.verylittlenightmares

URI uri = new URI(URL);
uri.getQuery();

I'll get this: id=eu.bandainamcoent.verylittlenightmares&hl=en

How to cut id= and &hl=en? And &hl=en may not exist and can be different, like &hl=de etc.

takotsubo
  • 492
  • 3
  • 14

1 Answers1

2

There are no function in URI to do this, but here is a trick to solve it:

Map<String, String> params = Arrays.stream(uri.getQuery().split("&"))
        .map(s -> s.split("="))
        .collect(Collectors.toMap(k -> k[0], v -> v.length > 1 ? v[1] : ""));
String id = params.get("id"); // eu.bandainamcoent.verylittlenightmares
String hl = params.get("hl"); // en

We are sure that the only separator between two parameters is &, and the parameter have a name and value separated by =, for that you split with &, and for each result split with =, and the result is a Map which contain the param name as key and value as value.

YCF_L
  • 51,266
  • 13
  • 85
  • 129