0

I tried to extract Sub-String from java string, but failed. 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 e.g. {123} and .mp3

For example audios/{211} new school poem.mp3 to new school poem

4
  • What do you mean by failed? Did you get an error? Commented May 29, 2019 at 19:16
  • 6
    If you don't want to include the /, use text.indexOf('/')+1. And better to use lastIndexOf('.') than relying on subtracting 2. Commented May 29, 2019 at 19:18
  • 3
    If you're working with file names, Apache Commons has FileNameUtils that do the hard work for you and would scale better. Commented May 29, 2019 at 19:19
  • Regex is overkill ... anyway, duplicate stackoverflow.com/questions/924394/… Commented May 29, 2019 at 19:42

4 Answers 4

1

This answer might helpful

String text = "audios/any_audio_name.mp3";
text= text.substring(text.indexOf('/')+1, text.indexOf('.'));
Sign up to request clarification or add additional context in comments.

4 Comments

this also work but previous one give me answer first
You'd probably want to use lastIndexOf for both of them. Whether changing that or not, you should also cross your fingers and hope to never see input like foo.bar/file_without_extension
Yes you correct but in here his requirement is to get name of mp3 file.so most probably that file come with .mp3 extention and he describe file path as audios
Actually i read XML data is in server in listview, first i filtered (whole line into name) and then showing it in listview.
1

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");
        }
    }
}

8 Comments

"^.*/(.*)\\..*$" would fit more cases. You can't use \/, it's either \\/ or /. And use group(1).
String pattern = "audios\/(.*).mp3"; give "Illegal escape character in string literal" error
I/System.out: NO MATCH
Simpler to just use replace, e.g. text = text.replaceFirst("^(?:.*?/)?([^/]+?)(?:\\.[^./]*?)?$", "$1"); --- See demo on regex101
@Attaullah 1) Learn regex. 2) Update the pattern in the replace call.
|
1

For Your Edited Question.Following Code segment will help you but here i assume that there will be no number within "any_audio_name"

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

2 Comments

but {} is remaining
.replaceAll("\\{|\\}|[\\d.]",""); is use this
0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.