06.01.2013 Views

Learning Processing: A Beginner's Guide to Programming Images ...

Learning Processing: A Beginner's Guide to Programming Images ...

Learning Processing: A Beginner's Guide to Programming Images ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

336 <strong>Learning</strong> <strong>Processing</strong><br />

When you send a URL path in<strong>to</strong> loadStrings( ) , you get back the raw HTML ( “ Hypertext Markup<br />

Language ” ) source of the requested web page. It is the same stuff that appears upon selecting “ View<br />

Source ” from a browser’s menu options. You do not need <strong>to</strong> be an HTML expert <strong>to</strong> follow this section,<br />

but if you are not familiar at all with HTML, you might read http://en.wikipedia.org/wiki/HTML .<br />

Unlike with the comma-delimited data from a text fi le that was specially formatted for use in a <strong>Processing</strong><br />

sketch, it is not practical <strong>to</strong> have the resulting raw HTML s<strong>to</strong>red in an array of Strings (each element<br />

representing one line from the source). Converting the array in<strong>to</strong> one long String can make things a bit<br />

simpler. As we saw earlier in the chapter, this can be achieved using join( ) .<br />

String onelongstring = join(lines, " " );<br />

When pulling raw HTML from a web page, it is likely you do not want all of the source, but just a small<br />

piece of it. Perhaps you are looking for weather information, a s<strong>to</strong>ck quote, or a news headline. We can<br />

take advantage of the String manipulation functions we learned— indexOf( ) , substring( ) , and length( ) —<br />

<strong>to</strong> fi nd pieces of data within a large block of text. We saw an early example of this in Exercise 18-2 . Take,<br />

for example, the following String:<br />

String stuff = " Number of apples:62. Boy, do I like apples or what! " ;<br />

Let’s say we want <strong>to</strong> pull out the number of apples from the above text. Our algorithm would be as<br />

follows:<br />

1.<br />

2.<br />

3.<br />

4.<br />

Find the end of the substring “ apples: ”. Call it start.<br />

Find the fi rst period after “ apples: ” . Call it end.<br />

Make a substring of the characters between start and end.<br />

Convert the String <strong>to</strong> a number (if we want <strong>to</strong> use it as such).<br />

In code, this look’s like:<br />

int start = stuff.indexOf( " apples: " ) + 8; // STEP 1<br />

int end = stuff.indexOf( " . " ,start); // STEP 2<br />

String apples = stuff.Substring(start,end); // STEP 3<br />

int apple_no = int(apples); // STEP 4<br />

Th e above code will do the trick, but we should be a bit more careful <strong>to</strong> make sure we do not run in<strong>to</strong><br />

any errors if we do not fi nd the substrings . We can add some error checking and generalize the code in<strong>to</strong> a<br />

function:<br />

// A function that returns a substring between two substrings<br />

String giveMeTextBetween(String s, String startTag, String endTag) {<br />

String found = " " ;<br />

// Find the index of the beginning tag<br />

int startIndex = s.indexOf(startTag);<br />

// If we don’t find anything<br />

if (startIndex = = –1) return " " ;<br />

// Move <strong>to</strong> the end of the beginning tag<br />

startIndex + = startTag.length();<br />

A function <strong>to</strong> return a String found<br />

between two Strings. If beginning<br />

or end “tag” is not found, the<br />

function returns an empty String.<br />

The “end” of a String<br />

can be found by<br />

searching for the index<br />

of that String and<br />

adding the length <strong>to</strong> that<br />

index (in this case 8).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!