문제

I have a String like this

String str = "www.google.com/..../upload/FileName.zip

I want to extract the string "FileName" from above string. I tried substr() method but couldn't found any solution.

도움이 되었습니까?

해결책

Is it what you are looking for ?

  String lastPart= yourUrl.substring(yourUrl.lastIndexOf('/')+1, yourUrl.length());
  String fileName = lastPart.substring(0, lastPart.lastIndexOf('.'));

Considering the given format won't change.

Demo

다른 팁

You can try this

    String str = "www.google.com/..../upload/FileName.zip";
    File file=new File(str);
    String fileName=file.getName();// now fileName=FileName.zip
    System.out.println(fileName.split("\\.")[0]);

Out put:

    FileName

To get the part between the last slash and the dot:

String filename = str.replaceAll(".*?(?:/(\\w+)\\.\\w+$)?", "$1");

This regex has some special sauce added to return a blank if the target isn't found, by making the target optional and the leading expression reluctant.

Try this

String str = "www.google.com/..../upload/FileName.zip";
    String  str1[] = str.split("/");
    String file=str1[str1.length-1];
    String fileName=file.substring(0, file.lastIndexOf("."));
    System.out.println(fileName);

Output

FileName
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top