Get file name from URL

前端 未结 27 1605
[愿得一人]
[愿得一人] 2020-11-28 02:24

In Java, given a java.net.URL or a String in the form of http://www.example.com/some/path/to/a/file.xml , what is the easiest way to g

27条回答
  •  庸人自扰
    2020-11-28 03:12

    This should about cut it (i'll leave the error handling to you):

    int slashIndex = url.lastIndexOf('/');
    int dotIndex = url.lastIndexOf('.', slashIndex);
    String filenameWithoutExtension;
    if (dotIndex == -1) {
      filenameWithoutExtension = url.substring(slashIndex + 1);
    } else {
      filenameWithoutExtension = url.substring(slashIndex + 1, dotIndex);
    }
    

提交回复
热议问题