-2

I want perform sub string operation on string. I have string <img src="file:///android_asset/img/pj1.jpg" />, and I want to get pj1.jpg; the name of image can be anything and I want the filename only.

I tried finame = mainname.substring(0,mainname.lastIndexOf(".")); but I am getting <img src="file:///android_asset/img/pj1

David Wasser
  • 93,459
  • 16
  • 209
  • 274

4 Answers4

0
try {
        String input= "<img src=\"file:///android_asset/img/pj1.jpg\" />";
        XmlPullParserFactory factory=XmlPullParserFactory.newInstance();
        XmlPullParser parse= factory.newPullParser();
        Reader reader= new StringReader(input);
        parse.setInput(reader);
        int eventType = parse.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            if(eventType == XmlPullParser.START_DOCUMENT) {

            } else if(eventType == XmlPullParser.START_TAG) {

                System.out.println("Start tag "+ parse.getAttributeValue(0));
                String val=parse.getAttributeValue(0);
                String arrTemp[] = val.split("/");
                System.out.println("your value"+arrTemp[arrTemp.length-1]);
            } else if(eventType == XmlPullParser.END_TAG) {

            } else if(eventType == XmlPullParser.TEXT) {

            }
            eventType = parse.next();
        }

    } catch (XmlPullParserException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

Works and tested . See if it helps.

0

Try something like this.

finame = mainname.substring(36,a.lastIndexOf("'"));
Birat Bade Shrestha
  • 800
  • 1
  • 8
  • 28
0

Since you have given HTML I believe you are trying to parse HTML. You can use jsoup for parsing html easily. Below is the sample code for the same text you have given. The src of the image tag can be easily obtained and using File we can easily get the fileName.

String htmlString= "<img src='file:///android_asset/img/pj1.jpg' />";
    Document doc=Jsoup.parse(htmlString);
    Elements imgs=doc.select("[src]");
    for(Element img : imgs){
        File f=new File(img.attr("src"));
        System.out.println(f.getName());
    }
balaaagi
  • 502
  • 11
  • 21
0

As a quick-and-dirty patch, something like:

String s1 = htmlImg.substring(0,htmlImg.lastIndexOf("\"")-1);
String fileName = htmlImg.substring(htmlImg.lastIndexOf("/"));
Mario
  • 1,661
  • 13
  • 22