/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
import java.net.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
{
ArrayList<String> urls = new ArrayList<String>();
urls.add(null);
urls.add("");
urls.add("file:///home/user/test.html");
urls.add("file:///home/user/test.html?id=902");
urls.add("file:///home/user/test.html#footer");
urls.add("http://e...content-available-to-author-only...e.com");
urls.add("http://w...content-available-to-author-only...e.com");
urls.add("http://w...content-available-to-author-only...e.txt");
urls.add("http://e...content-available-to-author-only...e.com/");
urls.add("http://e...content-available-to-author-only...e.com/a/b/c/test.html");
urls.add("http://e...content-available-to-author-only...e.com/a/b/c/test.html?param=value");
urls.add("http://e...content-available-to-author-only...e.com/a/b/c/test.html#anchor");
urls.add("http://e...content-available-to-author-only...e.com/a/b/c/test.html#anchor?param=value");
System.
out.
println("Input: \"" + url
+ "\" Output: \"" + getFileNameFromURL
(url
) + "\""); }
}
if (url == null) {
return "";
}
try {
String host
= resource.
getHost(); if (host.length() > 0 && url.endsWith(host)) {
// handle ...example.com
return "";
}
}
return "";
}
int startIndex = url.lastIndexOf('/') + 1;
int length = url.length();
// find end index for ?
int lastQMPos = url.lastIndexOf('?');
if (lastQMPos == -1) {
lastQMPos = length;
}
// find end index for #
int lastHashPos = url.lastIndexOf('#');
if (lastHashPos == -1) {
lastHashPos = length;
}
// calculate the end index
int endIndex
= Math.
min(lastQMPos, lastHashPos
); return url.substring(startIndex, endIndex);
}
}