0

In my Android application I have a large String containing the source of a webpage of mine. This webpage using javascript rotates a six transparent png's over the top of a few other png's. What I want to do is extract the last 4 characters of the png file names, but ONLY for the first 6 images that appear in the source code. These last 4 characters of each png file name are a time stamp that i need and want to read them into an array.

Source:

<html><head><style>html {background: black} body {background:black;margin:0;padding:0;}</style><script>setInterval('changeImage()',1000);function changeImage() {size = images.length;if (current == size-1) { current = -1;}current++;document.getElementById('rad').src = images[current];    }current = 0;images = new Array();images[0] = 'image.201105261318.png';images[1] = 'image.201105261323.png';images[2] = 'image.201105261330.png';images[3] = 'image.201105261336.png';images[4] = 'image.201105261342.png';images[5] = 'image.201105261348.png';</script></head><body><img style="position:absolute; top: 0px; left: 0px;z-index:1;" src="http://background.png"></body></html>

Desired output:

String Array = "1318,1323,1330,1336,1342,1348"

I have tried a lot of different substring retrieval methods, but still am struggling. I know how to identify the end of the string using ".png" but not sure how to say that i want just the 4 chars preceding that delimiter..?

Any help would be greatly appreciated.

Cheers

Greg

EDIT:

Got It!

int delimiter = source.indexOf(".png");
String imageTime1 = source.substring(delimiter-4,delimiter);
int delimiter2 = source.indexOf(".png",delimiter+4);
String imageTime2 = source.substring(delimiter2-4,delimiter2);
int delimiter3 = source.indexOf(".png",delimiter2+4);
String imageTime3 = source.substring(delimiter3-4,delimiter3);
int delimiter4 = source.indexOf(".png",delimiter3+4);
String imageTime4 = source.substring(delimiter4-4,delimiter4);
int delimiter5 = source.indexOf(".png",delimiter4+4);
String imageTime5 = source.substring(delimiter5-4,delimiter5);
int delimiter6 = source.indexOf(".png",delimiter5+4);
String imageTime6 = source.substring(delimiter6-4,delimiter6);

Cheers

2 Answers 2

1

Perhaps I am missing the point, but...

Given index x being the text.indexOf(".png"), you can simply use text.substring(x-4,x);.

Then you can do text.indexOf(".png",x+4) to get your next image end, and repeat the above.

If possible case variations are a concern you could change the entire string to upper or lower case before searching for your extension.

Sign up to request clarification or add additional context in comments.

2 Comments

Just curious though, how does "delimiter+4" move it to the next image in my string?
@Pelly: It's indexOf, which commences at x+4. So the string is searched for the next ".png".
1

Once you have your list of images, do a String.split(';') to get a list of images[0] = 'image.201105261318.png' items. Then you can get the length of each item and take a String.substring(length-9,length-4) to get the last 4 characters of each item.

The substring code is untested but you can tweak it to get exactly what you need.

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.