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)

1
  • BUild Regex and extract the content you want. use sites like this to build the regex. txt2re.com/… Commented Apr 3, 2014 at 12:30

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I have to use only pure string methods substring or split see tags
Your answer is also useful: But Url exactly need url format( should have http://)
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

7 Comments

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 - 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 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 - That would retrieve the site domain as well, which I don't think they want.
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.
|
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

Comments

-1
String s = "http://www.freegreatpicture.com/files/146/26189-abstract-color-background.jpg";
s = s.substring(s.indexOf("/", str.indexOf("/") + 1));

1 Comment

your result: /www.freegreatpicture.com/files/146/26189-abstract-color-background.jpg

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.