user1414304
user1414304

Reputation: 1

How to get a particular string from a sentence in Android?

I stored my data in a SQLite database. The data which I stored is in the HTML tags, for example, "Who is a good leader?"

Enter image description here

Actually, my task is to display a question and its related image. I kept my images in a drawable folder. Now when the question is displayed along with that the image it should also display. How do I achieve this task?

Upvotes: 0

Views: 269

Answers (1)

Shellum
Shellum

Reputation: 3179

If you really are constrained to parse html, a series of substring and indexOf calls will work assuming you have unique non-repeating markers in what you are parsing.

You could for example do this:

    //Set markers that will act as unique identifiers for when the image name begins/ends
    final String beginningMarker = "src=\"";
    final String endingMarker = "\"";

    //Define what html we are parsing
    String html = "<img alt=\"\" width=\"248\" height=\"170\" src=\"/test/image1.png\" />";

    //Use our markers to find out the locations in the html where the image name begins/ends
    int imgStringStartIndex = html.indexOf(beginningMarker) + beginningMarker.length();
    int imgStringEndIndex = html.indexOf(endingMarker, imgStringStartIndex);

    //Use the locations we just found to extract the image name
    String imageName = html.substring(imgStringStartIndex, imgStringEndIndex);
    System.out.println(imageName);

Upvotes: 1

Related Questions