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

Attaullah
  • 3,856
  • 3
  • 48
  • 63
  • What do you mean by failed? Did you get an error? – GBlodgett May 29 '19 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. – Andy Turner May 29 '19 at 19:18
  • 3
    If you're working with file names, Apache Commons has [FileNameUtils](https://commons.apache.org/proper/commons-io/javadocs/api-2.5/org/apache/commons/io/FilenameUtils.html) that do the hard work for you and would scale better. – Compass May 29 '19 at 19:19
  • Regex is overkill ... anyway, duplicate https://stackoverflow.com/questions/924394/how-to-get-the-filename-without-the-extension-in-java – second May 29 '19 at 19:42

4 Answers4

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

Qingfei Yuan
  • 1,196
  • 1
  • 8
  • 12
  • 2
    `"^.*/(.*)\\..*$"` would fit more cases. You can't use `\/`, it's either `\\/` or `/`. And use `group(1)`. – Paul Lemarchand May 29 '19 at 19:27
  • String pattern = "audios\/(.*).mp3"; give "Illegal escape character in string literal" error – Attaullah May 29 '19 at 19:30
  • I/System.out: NO MATCH – Attaullah May 29 '19 at 19:39
  • 1
    Simpler to just use replace, e.g. `text = text.replaceFirst("^(?:.*?/)?([^/]+?)(?:\\.[^./]*?)?$", "$1");` --- See [demo](https://regex101.com/r/6sz0lj/1) on regex101 – Andreas May 29 '19 at 19:39
  • @Andreas if text contains like audios/{123} audio_name.mp3 and i need just name like audio_name what will i do??? – Attaullah May 29 '19 at 19:50
  • 1
    @Attaullah 1) Learn regex. 2) Update the pattern in the replace call. – Andreas May 29 '19 at 19:53
  • Pattern miss some java string in List. when Capital MP3 instead of mp3 @Andreas – Attaullah May 29 '19 at 21:41
  • @Attaullah Since [my pattern](https://stackoverflow.com/questions/56367130/how-to-extract-a-substring-from-android-java-string/56367224?noredirect=1#comment99337877_56367224) doesn't specify `mp3`, it cannot care about lower- vs upper-case, so it simply cannot match differently just because `.mp3` is changed to `.MP3`. Try again. – Andreas May 29 '19 at 22:10
1

This answer might helpful

String text = "audios/any_audio_name.mp3";
text= text.substring(text.indexOf('/')+1, text.indexOf('.'));
Malinda
  • 524
  • 2
  • 5
  • 11
  • this also work but previous one give me answer first – Attaullah May 29 '19 at 19:45
  • 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` – Andreas May 29 '19 at 19:48
  • 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 – Malinda May 29 '19 at 19:53
  • Actually i read XML data is in server in listview, first i filtered (whole line into name) and then showing it in listview. – Attaullah May 29 '19 at 20:47
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.]", "");;
Malinda
  • 524
  • 2
  • 5
  • 11
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);
Attaullah
  • 3,856
  • 3
  • 48
  • 63