-1

I have a string like this :

https://storage.googleapis.com/migrasibo/banner/pm_banner_200617_2LLZ.png?GoogleAccessId=storage@alfagift-non-production-228103.iam.gserviceaccount.com&Expires=1592365768&Signature=egF4189zrTm4Lv%2FMioX%2FMSixttA2mkqWajUSfZlM1vquoovBZEpdp5vyJ0tKo7QPVfuiRw1Kmm7eWotJVL7kYypcHCSbq9lKECFI6t4t7RT%2BKVonkZrC0Ma%2Fd7tDR3mjcMjNPxkofMtO9NvDmGUTxJnHUwQ%2Bl%2BMzbShbCMziT%2FdAR0oRzGC940VKsIZHpXkiQzNaf1qPhrm%2F2JuSiddeOx2gdlaIy%2B%2FOaRK7SKTjLiqyNz0GIOqbNszcYeQ%2BCaenrKmxFe7b9xZMyp2c8uaRzlpSksNgf%2B7pswzIwuwF1wLl7WEHadyFVHVOgW8hGbRqJkK%2FQ9Yy%2B9xvCURVMWQdvw%3D%3D

I want to get the part of that url as this given below :

https://storage.googleapis.com/migrasibo/banner/pm_banner_200617_2LLZ.png

What should be the code to get that ?

Anish B.
  • 9,111
  • 3
  • 21
  • 41
hirarqi
  • 251
  • 2
  • 14
  • 3
    `urlStr = urlStr.substring(0, urlStr.indexOf("?"));` - here is explanation of `substring`- https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#substring(int,int) – KunLun Jun 22 '20 at 07:56
  • I think you should use regex in such cases – GoranLegenda Jun 22 '20 at 07:56
  • 1
    `substring`, not `subString`. – khelwood Jun 22 '20 at 07:59
  • Welcome. But be carful at `indexOf` it can return `-1` if there is no `?`, which will trigger an error at `substring`. To deal with that you need some condition. But if you want to keep it simple, I would recommend answer of Tim Biegeleisen, with regex. – KunLun Jun 22 '20 at 08:36
  • Yes, I understand. but for this case. String value "?" will always be there – hirarqi Jun 22 '20 at 09:19
  • Does this answer your question? [Getting parts of a URL (Regex)](https://stackoverflow.com/questions/27745/getting-parts-of-a-url-regex) – mohammad asghari Jun 22 '20 at 09:24

2 Answers2

2

A quick and dirty solution would be to just remove the query string (if it exists) using a regex replacement:

String url = "https://storage.googleapis.com/migrasibo/banner/pm_banner_200617_2LLZ.png?GoogleAccessId=storage@alfagift-non-production-228103.iam.gserviceaccount.com&Expires=1592365768&Signature=egF4189zrTm4Lv%2FMioX%2FMSixttA2mkqWajUSfZlM1vquoovBZEpdp5vyJ0tKo7QPVfuiRw1Kmm7eWotJVL7kYypcHCSbq9lKECFI6t4t7RT%2BKVonkZrC0Ma%2Fd7tDR3mjcMjNPxkofMtO9NvDmGUTxJnHUwQ%2Bl%2BMzbShbCMziT%2FdAR0oRzGC940VKsIZHpXkiQzNaf1qPhrm%2F2JuSiddeOx2gdlaIy%2B%2FOaRK7SKTjLiqyNz0GIOqbNszcYeQ%2BCaenrKmxFe7b9xZMyp2c8uaRzlpSksNgf%2B7pswzIwuwF1wLl7WEHadyFVHVOgW8hGbRqJkK%2FQ9Yy%2B9xvCURVMWQdvw%3D%3D";
url = url.replaceAll("\\?.*$", "");
System.out.println(url);

This prints:

https://storage.googleapis.com/migrasibo/banner/pm_banner_200617_2LLZ.png
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
2

Try this :

String content="https://storage.googleapis.com/migrasibo/banner/pm_banner_200617_2LLZ.png?GoogleAccessId=storage@alfagift-non-production-228103.iam.gserviceaccount.com&Expires=1592365768&Signature=egF4189zrTm4Lv%2FMioX%2FMSixttA2mkqWajUSfZlM1vquoovBZEpdp5vyJ0tKo7QPVfuiRw1Kmm7eWotJVL7kYypcHCSbq9lKECFI6t4t7RT%2BKVonkZrC0Ma%2Fd7tDR3mjcMjNPxkofMtO9NvDmGUTxJnHUwQ%2Bl%2BMzbShbCMziT%2FdAR0oRzGC940VKsIZHpXkiQzNaf1qPhrm%2F2JuSiddeOx2gdlaIy%2B%2FOaRK7SKTjLiqyNz0GIOqbNszcYeQ%2BCaenrKmxFe7b9xZMyp2c8uaRzlpSksNgf%2B7pswzIwuwF1wLl7WEHadyFVHVOgW8hGbRqJkK%2FQ9Yy%2B9xvCURVMWQdvw%3D%3D";

System.out.println(content.split("\\?")[0]);

Output :

https://storage.googleapis.com/migrasibo/banner/pm_banner_200617_2LLZ.png
Anish B.
  • 9,111
  • 3
  • 21
  • 41