سؤال

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