0

I'm getting the following url string path for an example: the following string url format /version/mydownload?a=abc&b=bbb&c=ccc My question, how would validate? would you split / and & and then loop or is there a better way to validate?

my url string is not included http or www but it's part of the url string as shown above, would you split the string and check to see if this is in a right format?

Nick Kahn
  • 19,652
  • 91
  • 275
  • 406
  • The correct solution is to use a URL / URI parser; see https://stackoverflow.com/questions/13592236/. Validating URLs by hand is really tricky because of the various special cases that you need to deal with. – Stephen C Mar 24 '21 at 00:50
  • I have a specific string format(url) that need to validate if that string format is passed or not. – Nick Kahn Mar 24 '21 at 00:54
  • It’s always going to be the same format (url) – Nick Kahn Mar 24 '21 at 00:55
  • 1
    Using a parser is still the correct solution ... unless your benchmarking / profiling tells you that this validation *really* a performance bottleneck. (Don't optimize prematurely ...) – Stephen C Mar 24 '21 at 00:57
  • @NickKahn Sorry, but what do you mean by validate? Are you trying to get a parameter, verify it's a valid url or...? – Spectric Mar 24 '21 at 01:00
  • yes the key/value of the string, so for an example I just want to validate I have this format returns `/version/mydownload?a=abc&b=bbb&c=ccc` so the keys are `/version/mydownload` `a=abc` `b=bbb` `c=ccc` for an exaple. – Nick Kahn Mar 24 '21 at 01:11
  • 1
    @StephenP no it does not answer my question and again I'm not validating entire url, just validating part of a url string. – Nick Kahn Mar 24 '21 at 01:28

1 Answers1

0

I'm sure using a URL parser is a great way to parse URLs, but seeing as this really only requires some basic string splitting it seems like an overkill.

The following code should work:

String url = "/version/mydownload?a=abc&b=bbb&c=ccc";
int c1 = url.indexOf('?');
String path = c1 != -1 ? url.substring(0, c1) : url;
String[] keys = url.substring(c1+1).split("&");
System.out.println("Path: "+path+", keys: "+Arrays.toString(keys));

To extract the key value pairs, just split the String by = like so:

String firstKey = keys[0];
int c2 = firstKey.indexOf('=');
String key = firstKey.substring(0, c2);
String value = firstKey.substring(c2+1);

Which prints:

Path: /version/mydownload, keys: [a=abc, b=bbb, c=ccc]
Spectric
  • 30,714
  • 6
  • 20
  • 43
  • It's more complicated than this. For instance, what comes after `?` must be key value pairs, the path before `?` must be a valid, well, path, etc. – jingx Mar 24 '21 at 03:50
  • @jingx To me it looked like all OP wanted to do was extract `/version/mydownload` `a=abc` `b=bbb` `c=ccc`, in [this comment](https://stackoverflow.com/questions/66773201/how-do-i-validate-a-string-format-in-java/66773548?noredirect=1#comment118035595_66773201) – Spectric Mar 24 '21 at 03:53
  • Yeah but when people start and end their narrative with "for example", you don't really know what exactly the rules or possible inputs are. I doubt OP knows either, all the more reason to go with a URL parser. – jingx Mar 24 '21 at 04:06