Pooh program example 03-stringlib.p
# tests string library functions p = 'the quick brown fox jumped' q = 'fox' n = find( ~hay p ~needle q ) println( ~msg 'Position "[ n ]" of "[ q ]" in "[ p ]"' ) q = 'bee' n = find( ~hay p ~needle q ) println( ~msg 'Position "[ n ]" of "[ q ]" in "[ p ]"' ) a = left( ~string 'abcdefg' ~length 3 ) println( ~msg a ) a = left( ~string 'abcdefg' ~length 200 ) println( ~msg a ) a = right( ~string 'abcdefg' ~length 3 ) println( ~msg a ) a = right( ~string 'abcdefg' ~length 200 ) println( ~msg a ) a = mid( ~string 'abcdefg' ~offset 2 ~length 3 ) println( ~msg a ) a = mid( ~string 'abcdefg' ~offset 2 ) println( ~msg a ) a = mid( ~string 'abcdefg' ~length 3 ) println( ~msg a ) a = mid( ~string 'abcdefg' ~offset -1 ~length 4 ) println( ~msg a )
Position "17" of "fox" in "the quick brown fox jumped" Position "0" of "bee" in "the quick brown fox jumped" abc abcdef efg abcdefg bcd bcdefg abc abTrace output for 03-stringlib.p
003|p = 'the quick brown fox jumped' 004|q = 'fox' 005|... = find( ~hay p:'the quick brown fox jumped' ~needle q:'fox' )... 005|n = find( ~hay p:'the quick brown fox jumped' ~needle q:'fox' ):17 006|println( ~msg 'Position "' .. n:17 .. '" of "' .. q:'fox' .. '" in "' .. p:'the quick brown fox jumped' .. '"' )... 008|q = 'bee' 009|... = find( ~hay p:'the quick brown fox jumped' ~needle q:'bee' )... 009|n = find( ~hay p:'the quick brown fox jumped' ~needle q:'bee' ):0 010|println( ~msg 'Position "' .. n:0 .. '" of "' .. q:'bee' .. '" in "' .. p:'the quick brown fox jumped' .. '"' )... 012|... = left( ~string 'abcdefg' ~length 3 )... 012|a = left( ~string 'abcdefg' ~length 3 ):'abc' 013|println( ~msg a:'abc' )... 015|... = left( ~string 'abcdefg' ~length 200 )... 015|a = left( ~string 'abcdefg' ~length 200 ):'abcdef' 016|println( ~msg a:'abcdef' )... 018|... = right( ~string 'abcdefg' ~length 3 )... 018|a = right( ~string 'abcdefg' ~length 3 ):'efg' 019|println( ~msg a:'efg' )... 021|... = right( ~string 'abcdefg' ~length 200 )... 021|a = right( ~string 'abcdefg' ~length 200 ):'abcdefg' 022|println( ~msg a:'abcdefg' )... 024|... = mid( ~string 'abcdefg' ~offset 2 ~length 3 )... 024|a = mid( ~string 'abcdefg' ~offset 2 ~length 3 ):'bcd' 025|println( ~msg a:'bcd' )... 027|... = mid( ~string 'abcdefg' ~offset 2 )... 027|a = mid( ~string 'abcdefg' ~offset 2 ):'bcdefg' 028|println( ~msg a:'bcdefg' )... 030|... = mid( ~string 'abcdefg' ~length 3 )... 030|a = mid( ~string 'abcdefg' ~length 3 ):'abc' 031|println( ~msg a:'abc' )... 033|... = mid( ~string 'abcdefg' ~offset - 1 ~length 4 )... 033|a = mid( ~string 'abcdefg' ~offset - 1 ~length 4 ):'ab' 034|println( ~msg a:'ab' )...