简体   繁体   English

如何从Android Java String中提取subString?

[英]How to extract a subString from android java String?

I tried to extract Sub-String from java string, but failed. 我试图从Java字符串中提取Sub-String,但是失败了。 i tried like this 我尝试过这样

String text = "audios/{any_number} any_audio_name.mp3";
text= text.substring(text.indexOf('/'), text.indexOf('3')-2);

Updated 更新

I need String contains only any_audio_name and removing audios/ , any number eg {123} and .mp3 我需要String只包含any_audio_name并删除any_audio_name audios/ ,任何数字,例如{123}.mp3

For example audios/{211} new school poem.mp3 to new school poem 例如, audios/{211} new school poem.mp3new school poem

Use regex seems fit here. 使用正则表达式似乎适合这里。

public class MyMain{
    public static void main(String args[]) {
        String line = "audios/any_audio_name.mp3";
        String pattern = "audios\\/(.*)\\.mp3";

        // Create a Pattern object
        Pattern r = Pattern.compile(pattern);

        // Now create matcher object.
        Matcher m = r.matcher(line);

        if (m.find()) {
            System.out.println("Found value: " + m.group(1));
        } else {
            System.out.println("NO MATCH");
        }
    }
}

This answer might helpful 这个答案可能有帮助

String text = "audios/any_audio_name.mp3";
text= text.substring(text.indexOf('/')+1, text.indexOf('.'));

For Your Edited Question.Following Code segment will help you but here i assume that there will be no number within "any_audio_name" 对于您已编辑的Question.Following Code片段将为您提供帮助,但在此我假设“ any_audio_name”中没有数字

String text = "audios/{any_number}any_audio_name.mp3";
text= text.substring(text.indexOf('/')+1, text.indexOf('.')).replaceAll("[\\d.]", "");;

Thanks for everyone, who give me idea for solving this issue. 谢谢大家,谁给我解决这个问题的想法。

 String text = "audios/{any_number}any_audio_name.mp3";
 text= text.substring(text.indexOf('/')+1, text.indexOf('.')).replaceAll("\\{|\\}|[\\d.]","");
 System.out.println(text);

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

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