简体   繁体   English

在某个字符的最后一次出现处拆分字符串

[英]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."因此,当该字符串出现时,它会在“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.如何在文件扩展名处拆分字符串,但在文件没有扩展名时也能够处理和警告。

You can try this你可以试试这个

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

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.有关正则表达式模式,请参阅Java 文档 Remember to escape the backslash because the pattern string needs the backslash.记住要转义反斜杠,因为模式字符串需要反斜杠。

Is this Java?这是Java吗? If so, why don't you use "java.io.File.getName".如果是这样,为什么不使用“java.io.File.getName”。

For example:例如:

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

Out:出去:

ccc.txt

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) (?=[^.]*$) - 确保之后的所有内容都不是句点,而不包括在匹配中)

How about splitting the filPath using the period as separator.如何使用句点作为分隔符拆分 filPath。 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];
        }

For an arbitrary splitting string c of any length:对于任意长度的任意拆分字符串c

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

You can use StringUtils from apache commons, which is an elegant way of extracting file type.您可以使用 apache commons 中的 StringUtils,这是一种提取文件类型的优雅方法。

    String example = "/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 format = StringUtils.substringAfterLast(example, ".");
    System.out.println(format);

Program will print "mp3" in the console.程序将在控制台中打印“mp3”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM