'Split string on the last occurrence of some character

I'm basically trying to split a string on the last period to capture the file extension. But sometimes the file doesn't have any 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."

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.



Solution 1:[1]

You can try this

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

Solution 2:[2]

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

For example:

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

Out:

ccc.txt

Solution 3:[3]

You can use a positive lookahead in your regex to ensure it only splits on the last occurrence. The positive lookahead ensures that it only splits when it does not see another occurrence later in the string.

// Using example filePath from question
String filePath = "/mnt/sdcard/OG Ron C, Chopstars & Drake - Choppin Ain't The Same-2013-MIXFIEND/02 Drake - Connect (Feat. Fat Pat) (Chopped Not Slopped).mp3";
String[] parts = filePath.split("\\.(?=[^.]*$)");
// parts = [
//     "/mnt/sdcard/OG Ron C, Chopstars & Drake - Choppin Ain't The Same-2013-MIXFIEND/02 Drake - Connect (Feat. Fat Pat) (Chopped Not Slopped)"
//     "mp3"
// ]

Breaking down the regex:

  • \\. - Find a period
  • (?=[^.]*$) - make sure everything after is not a period, without including it in the match)

Solution 4:[4]

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

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

Solution 5:[5]

For an arbitrary splitting string c of any length:

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Evgeniy Dorofeev
Solution 2 Kei Minagawa
Solution 3 Jon Senchyna
Solution 4 Juned Ahsan
Solution 5 Jose Duarte