-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

2
  • 6
    Did you look stackoverflow.com/questions/14526260/… Commented Jan 11, 2016 at 10:44
  • 1
    Retagged as this has nothing to do with Android. It is a Java language question. Commented Jan 11, 2016 at 10:54

4 Answers 4

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());
    }
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

0

Try something like this.

finame = mainname.substring(36,a.lastIndexOf("'"));

Comments

0

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

String s1 = htmlImg.substring(0,htmlImg.lastIndexOf("\"")-1);
String fileName = htmlImg.substring(htmlImg.lastIndexOf("/"));

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.