1

I have an URL address like: http://myfile.com/File1/beauty.png
I have to remove http://site address/ from main string
That mean result should be File1/beauty.png

Note: site address might be anything(e.g some.com, some.org)

Surendra Jnawali
  • 3,190
  • 5
  • 28
  • 44
  • BUild Regex and extract the content you want. use sites like this to build the regex. http://txt2re.com/index-java.php3?s=http%3A%2F%2Fmyfile.org%2FFile1%2Fbeauty.png&submit=Show+Matches – Mani Apr 03 '14 at 12:30

4 Answers4

3

See here: http://docs.oracle.com/javase/tutorial/networking/urls/urlInfo.html

Just create a URL object out of your string and use URL.getPath() like this:

String s = new URL("http://myfile.com/File1/beauty.png").getPath();

If you don't need the slash at the beginning, you can remove it via s.substring(1, s.length());

Edit, according to comment:

If you are not allowed to use URL, this would be your best bet: Extract main domain name from a given url

See the accepted answer. Basically you have to get a TLD list, find the domain and substract everything till the domain names' end.

Community
  • 1
  • 1
INK
  • 235
  • 1
  • 11
2

If, as you say, you only want to use the standard String methods then this should do it.

public static String getPath(String url){
        if(url.contains("://")){
            url = url.substring(url.indexOf("://")+3);
            url = url.substring(url.indexOf("/") + 1);
        } else {
            url = url.substring(url.indexOf("/")+1);
        }
        return url;
    }

If the url contains :// then we know that the string you are looking for will come after the third /. Otherwise, it should come after the first. If we do the following;

System.out.println(getPath("http://myfile.com/File1/beauty.png"));
System.out.println(getPath("https://myfile.com/File1/beauty.png"));
System.out.println(getPath("www1.myfile.com/File1/beauty.png"));
System.out.println(getPath("myfile.co.uk/File1/beauty.png"));;

The output is;

File1/beauty.png
File1/beauty.png
File1/beauty.png
File1/beauty.png
Rudi Kershaw
  • 12,332
  • 7
  • 52
  • 77
  • I like your answer. Maybe you should extend getPath() to check, if it is prefixed by "://" and filter that out before extracting the string. This would be more failsafe - and you do not even need a loop to extract the string. Never the less - I'll give you my upvote :) – INK Apr 03 '14 at 12:20
  • @INK - That's a good idea. I might test it with a series of URLs and see how that goes. If all works out I'll update the answer. – Rudi Kershaw Apr 03 '14 at 12:21
  • @Rudi Great :) You should consider removing the loop and replace it with: url = url.substring(url.indexOf("://")+3); (haven't tested it, but it should work). Just a performance thing though :) – INK Apr 03 '14 at 12:33
  • @INK - That would retrieve the site domain as well, which I don't think they want. – Rudi Kershaw Apr 03 '14 at 12:36
  • 1
    Well, today is really not my day :D What about this one: url = url.substring(url.indexOf("://")+3); url = url.substring(url.indexOf("/") + 1); ? I just forgot the second line of code. – INK Apr 03 '14 at 12:38
  • @SurendraJnawali - I removed the loop from the answer a little while ago. That address should work with the method both before and after the change. And it will work no matter how many addresses with "http://" you pass in. – Rudi Kershaw Apr 03 '14 at 13:01
  • @SurendraJnawali Now it's perfect. Why is it not the accepted answer anymore? :( – INK Apr 03 '14 at 13:02
0

You can use the below approach to fetch the required data.

    String url = "http://myfile.org/File1/beauty.png";
    URL u = new URL(url);
    String[] arr = url.split(u.getAuthority());

    System.out.println(arr[1]);

    Output - /File1/beauty.png
-1
String s = "http://www.freegreatpicture.com/files/146/26189-abstract-color-background.jpg";
s = s.substring(s.indexOf("/", str.indexOf("/") + 1));
user2075328
  • 423
  • 2
  • 7
  • 16