fork(1) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.net.*;
  7.  
  8. /* Name of the class has to be "Main" only if the class is public. */
  9. class Ideone
  10. {
  11. public static void main (String[] args) throws java.lang.Exception
  12. {
  13. ArrayList<String> urls = new ArrayList<String>();
  14. urls.add(null);
  15. urls.add("");
  16. urls.add("file:///home/user/test.html");
  17. urls.add("file:///home/user/test.html?id=902");
  18. urls.add("file:///home/user/test.html#footer");
  19. urls.add("http://e...content-available-to-author-only...e.com");
  20. urls.add("http://w...content-available-to-author-only...e.com");
  21. urls.add("http://w...content-available-to-author-only...e.txt");
  22. urls.add("http://e...content-available-to-author-only...e.com/");
  23. urls.add("http://e...content-available-to-author-only...e.com/a/b/c/test.html");
  24. urls.add("http://e...content-available-to-author-only...e.com/a/b/c/test.html?param=value");
  25. urls.add("http://e...content-available-to-author-only...e.com/a/b/c/test.html#anchor");
  26. urls.add("http://e...content-available-to-author-only...e.com/a/b/c/test.html#anchor?param=value");
  27.  
  28. for (String url : urls) {
  29. System.out.println("Input: \"" + url + "\" Output: \"" + getFileNameFromURL(url) + "\"");
  30. }
  31.  
  32. }
  33.  
  34. public static String getFileNameFromURL(String url) {
  35. if (url == null) {
  36. return "";
  37. }
  38. try {
  39. URL resource = new URL(url);
  40. String host = resource.getHost();
  41. if (host.length() > 0 && url.endsWith(host)) {
  42. // handle ...example.com
  43. return "";
  44. }
  45. }
  46. return "";
  47. }
  48.  
  49. int startIndex = url.lastIndexOf('/') + 1;
  50. int length = url.length();
  51.  
  52. // find end index for ?
  53. int lastQMPos = url.lastIndexOf('?');
  54. if (lastQMPos == -1) {
  55. lastQMPos = length;
  56. }
  57.  
  58. // find end index for #
  59. int lastHashPos = url.lastIndexOf('#');
  60. if (lastHashPos == -1) {
  61. lastHashPos = length;
  62. }
  63.  
  64. // calculate the end index
  65. int endIndex = Math.min(lastQMPos, lastHashPos);
  66. return url.substring(startIndex, endIndex);
  67. }
  68. }
Success #stdin #stdout 0.1s 27676KB
stdin
Standard input is empty
stdout
Input: "null" Output: ""
Input: "" Output: ""
Input: "file:///home/user/test.html" Output: "test.html"
Input: "file:///home/user/test.html?id=902" Output: "test.html"
Input: "file:///home/user/test.html#footer" Output: "test.html"
Input: "http://e...content-available-to-author-only...e.com" Output: ""
Input: "http://w...content-available-to-author-only...e.com" Output: ""
Input: "http://w...content-available-to-author-only...e.txt" Output: ""
Input: "http://e...content-available-to-author-only...e.com/" Output: ""
Input: "http://e...content-available-to-author-only...e.com/a/b/c/test.html" Output: "test.html"
Input: "http://e...content-available-to-author-only...e.com/a/b/c/test.html?param=value" Output: "test.html"
Input: "http://e...content-available-to-author-only...e.com/a/b/c/test.html#anchor" Output: "test.html"
Input: "http://e...content-available-to-author-only...e.com/a/b/c/test.html#anchor?param=value" Output: "test.html"