在最后出现的字符上分割字符串

时间:2021-09-05 07:47:23

I'm basically trying to spilt a string on the last period to capture the file extension. But Sometimes the file has NO extension, so i'm anticipating that. But the problem is that some file names have periods before the end like so...

我基本上是想在最后一段时间溢出一个字符串来捕获文件扩展名。但是有时候这个文件没有扩展名,所以我预计到了。但问题是,有些文件的名称在结束前有段时间……

            /mnt/sdcard/OG Ron C, Chopstars & Drake - Choppin Ain't The Same-2013-MIXFIEND/02 Drake - Connect (Feat. Fat Pat) (Chopped Not Slopped).mp3

So when that string comes up it chops it at "02 Drake - Connect (Feat."

因此,当这个字符串出现时,它会在“02德雷克连接(专长)”上砍下它。

This is what I've been using...

这就是我一直在用的……

            String filePath = intent.getStringExtra(ARG_FILE_PATH);
    String fileType = filePath.substring(filePath.length() - 4);
    String FileExt = null;
    try {
        StringTokenizer tokens = new StringTokenizer(filePath, ".");
        String first = tokens.nextToken();
        FileExt = tokens.nextToken();
    }catch(NoSuchElementException e){
        customToast("the scene you chose, has no extension :(");
    }
    System.out.println("EXT " +FileExt);
    File fileToUpload = new File(filePath);

How do I split the string at the file extension but also be able to handle and alert when the file has no extension.

如何在文件扩展名中分割字符串,但在文件没有扩展时也可以处理和警告。

4 个解决方案

#1


26  

It might be easier to just assume that files which end with a dot followed by alphanumeric characters have extensions.

简单地假设以点结尾以字母数字字符结尾的文件具有扩展名可能更容易。

int p=filePath.lastIndexOf(".");
String e=filePath.substring(p+1);
if( p==-1 || !e.matches("\\w+") ){/* file has no extension */}
else{ /* file has extension e */ }

See the Java docs for regular expression patterns. Remember to escape the backslash because the pattern string needs the backslash.

有关正则表达式模式,请参阅Java文档。记住要避免反斜杠,因为模式字符串需要反斜杠。

#2


32  

You can try this

你可以试试这个

int i = s.lastIndexOf(c);
String[] a =  {s.substring(0, i), s.substring(i)};

#3


6  

Is this Java? If so, why don't you use "java.io.File.getName".

这是Java吗?如果是的话,为什么不使用“java.io.File.getName”呢?

For example:

例如:

File f = new File("/aaa/bbb/ccc.txt");
System.out.println(f.getName());

Out:

:

ccc.txt

#4


0  

How about splitting the filPath using the period as separator. And taking the last item in that array to get the extension:

用句号作为分隔符分割filPath怎么样?然后取数组中的最后一个项得到扩展名:

        String fileTypeArray[] = filePath.split(",");
        String fileType = "";
        if(fileTypeArray != null && fileTypeArray.length > 0) {
          fileType = fileTypeArray[fileTypeArray.length - 1];
        }

#1


26  

It might be easier to just assume that files which end with a dot followed by alphanumeric characters have extensions.

简单地假设以点结尾以字母数字字符结尾的文件具有扩展名可能更容易。

int p=filePath.lastIndexOf(".");
String e=filePath.substring(p+1);
if( p==-1 || !e.matches("\\w+") ){/* file has no extension */}
else{ /* file has extension e */ }

See the Java docs for regular expression patterns. Remember to escape the backslash because the pattern string needs the backslash.

有关正则表达式模式,请参阅Java文档。记住要避免反斜杠,因为模式字符串需要反斜杠。

#2


32  

You can try this

你可以试试这个

int i = s.lastIndexOf(c);
String[] a =  {s.substring(0, i), s.substring(i)};

#3


6  

Is this Java? If so, why don't you use "java.io.File.getName".

这是Java吗?如果是的话,为什么不使用“java.io.File.getName”呢?

For example:

例如:

File f = new File("/aaa/bbb/ccc.txt");
System.out.println(f.getName());

Out:

:

ccc.txt

#4


0  

How about splitting the filPath using the period as separator. And taking the last item in that array to get the extension:

用句号作为分隔符分割filPath怎么样?然后取数组中的最后一个项得到扩展名:

        String fileTypeArray[] = filePath.split(",");
        String fileType = "";
        if(fileTypeArray != null && fileTypeArray.length > 0) {
          fileType = fileTypeArray[fileTypeArray.length - 1];
        }