More Strings |
What if we want to collect links from web pages:
method findLinks(txt: String) -> String { var tagStart: Number var tagEnd: Number := 0 def lowerPage: String = html.asLower var tag: String var links: String := "" var fullPage := txt ++">" // make sure end search succeeds var done: Boolean := false while {true} do { tagStart := lowerPage.indexOf("<a ")startingAt(tagEnd+1)ifAbsent{ return links } tagEnd := lowerPage.indexOf(">")startingAt(tagStart+3) ifAbsent{ return links } tag := fullPage.substringFrom(tagStart)to(tagEnd) links := links++tag++"\n" } }
Notice how in the above, we search through a version of the string that is all lower case (to make it easy to detect "<a " even if capitalized, but report the string from the original.
Finally, if we want to determine whether something is composed only of digits - i.e., represents an integer:
method isInteger(word:String) -> Boolean { for(word) do {letter: String -> if (("0" > letter) || (letter > "9")) then { return false } } true }
More Strings |