Showing the changes to examples/string (6 of 486 files). Show all
Unified · Split
| @@ -0,0 +1,22 @@ | |||
| 1 | // escape sequences | ||
| 2 | |||
| 3 | // newline | ||
| 4 | <<< "line1\nline2", "\n" >>>; | ||
| 5 | |||
| 6 | // tab | ||
| 7 | <<< "a\tb", "\n" >>>; | ||
| 8 | |||
| 9 | // audible bell (uh...) | ||
| 10 | <<< "bell!\a", "\n" >>>; | ||
| 11 | |||
| 12 | // " | ||
| 13 | <<< "->\"<-", "\n" >>>; | ||
| 14 | |||
| 15 | // end of string | ||
| 16 | <<< "here\0can't see me", "\n" >>>; | ||
| 17 | |||
| 18 | // wait a bit | ||
| 19 | 200::ms => now; | ||
| 20 | |||
| 21 | // octal (man ascii) | ||
| 22 | <<< "bell!\007\060---\061\012", "" >>>; | ||
| @@ -0,0 +1,26 @@ | |||
| 1 | // this is really hacked: ConsoleInput class to be replaced soon | ||
| 2 | |||
| 3 | // make a ConsoleInput | ||
| 4 | ConsoleInput in; | ||
| 5 | // tokenizer | ||
| 6 | StringTokenizer tok; | ||
| 7 | |||
| 8 | // loop | ||
| 9 | while( true ) | ||
| 10 | { | ||
| 11 | // prompt | ||
| 12 | in.prompt( "enter line of text:" ) => now; | ||
| 13 | |||
| 14 | // read | ||
| 15 | while( in.more() ) | ||
| 16 | { | ||
| 17 | // get it | ||
| 18 | tok.set( in.getLine() ); | ||
| 19 | // print tokens | ||
| 20 | while( tok.more() ) | ||
| 21 | { | ||
| 22 | // print it | ||
| 23 | <<< tok.next(), "" >>>; | ||
| 24 | } | ||
| 25 | } | ||
| 26 | } | ||
| @@ -0,0 +1,54 @@ | |||
| 1 | // three strings | ||
| 2 | "hello" => string foo; | ||
| 3 | "hello" => string bar; | ||
| 4 | "there" => string geh; | ||
| 5 | |||
| 6 | // makeshift assert | ||
| 7 | fun void assert( int condition, string text ) | ||
| 8 | { | ||
| 9 | if( !condition ) | ||
| 10 | { | ||
| 11 | <<< "assertion failed: ", text >>>; | ||
| 12 | me.exit(); | ||
| 13 | } | ||
| 14 | } | ||
| 15 | |||
| 16 | // equality | ||
| 17 | assert( foo == foo, "1" ); | ||
| 18 | assert( foo == bar, "2" ); | ||
| 19 | assert( "abc" == "abc", "3" ); | ||
| 20 | assert( "hello" == foo, "4" ); | ||
| 21 | |||
| 22 | assert( foo != geh, "5" ); | ||
| 23 | assert( "x" != "y", "6" ); | ||
| 24 | assert( foo != "there", "7" ); | ||
| 25 | |||
| 26 | // lt | ||
| 27 | assert( foo < geh, "8" ); | ||
| 28 | assert( foo < "hello!", "9" ); | ||
| 29 | assert( foo <= foo, "10" ); | ||
| 30 | assert( foo <= "there", "11" ); | ||
| 31 | |||
| 32 | // gt | ||
| 33 | assert( foo > "abc", "12" ); | ||
| 34 | assert( foo > "b", "13" ); | ||
| 35 | assert( foo >= foo, "14" ); | ||
| 36 | assert( foo >= bar, "15" ); | ||
| 37 | |||
| 38 | // concatention | ||
| 39 | assert( "foo" + "bar" == "foobar", "16" ); | ||
| 40 | "foo" => string s; | ||
| 41 | "bar" +=> s; | ||
| 42 | assert( s == "foobar", "17" ); | ||
| 43 | assert( "bar" + 10 == "bar10", "18" ); | ||
| 44 | assert( "bar" + 10.0 == "bar10.0000", "19" ); | ||
| 45 | assert( 10 + "bar" == "10bar", "20" ); | ||
| 46 | assert( 10.0 + "bar" == "10.0000bar", "21" ); | ||
| 47 | assert( "foo" + "bar" + "cle" == "foobarcle", "22" ); | ||
| 48 | assert( "FoO".lower() == "foo", "23" ); | ||
| 49 | assert( "foo".upper() == "FOO", "24" ); | ||
| 50 | assert( " foo ".trim() == "foo", "25" ); | ||
| 51 | assert( " foo ".ltrim() == "foo ", "26" ); | ||
| 52 | assert( " foo ".rtrim() == " foo", "27" ); | ||
| 53 | |||
| 54 | <<< "success" >>>; | ||
| @@ -0,0 +1,52 @@ | |||
| 1 | // a string | ||
| 2 | "the quick brown fox jumped over the lazy dog" @=> string str; | ||
| 3 | |||
| 4 | // print string | ||
| 5 | <<< "str:", str >>>; | ||
| 6 | |||
| 7 | // read the first char (as int) | ||
| 8 | <<< "str.charAt( 0 ):", str.charAt( 0 ) >>>; | ||
| 9 | // read 10th char | ||
| 10 | <<< "str.charAt( 10 ):", str.charAt( 10 ) >>>; | ||
| 11 | // test character by index | ||
| 12 | str.setCharAt( 0, 'T' ); | ||
| 13 | <<< "str.setCharAt( 0, 'T' ): ", str >>>; | ||
| 14 | |||
| 15 | // test substring -- from the 10th char | ||
| 16 | <<< "str.substring( 10 ):", str.substring( 10 ) >>>; | ||
| 17 | // test substring -- from 10th char, for 10 chars | ||
| 18 | <<< "str.substring( 10, 10 ):", str.substring(10, 10) >>>; | ||
| 19 | |||
| 20 | // insert | ||
| 21 | str.insert( 36, "old " ); | ||
| 22 | <<< "str.insert( 36, \"old \" ):", str >>>; | ||
| 23 | |||
| 24 | // test erase | ||
| 25 | str.erase( 40, 5 ); | ||
| 26 | <<< "str.erase( 40, 5 ):", str >>>; | ||
| 27 | |||
| 28 | // test replace | ||
| 29 | str.replace( 40, "cat" ); | ||
| 30 | <<< "str.replace( 45, \"cat\" ):", str >>>; | ||
| 31 | // test replace | ||
| 32 | str.replace( 4, 4, "slow" ); | ||
| 33 | <<< "str.replace( 4, 5, \"slow\" ):", str >>>; | ||
| 34 | |||
| 35 | // test find -- index of letter, return -1 if not found | ||
| 36 | <<< "str.find('b'):", str.find('b') >>>; | ||
| 37 | // test find | ||
| 38 | <<< "str.find('b', 15):", str.find('b', 15) >>>; | ||
| 39 | // test find | ||
| 40 | <<< "str.find(\"fox\"):", str.find("fox") >>>; | ||
| 41 | // test find | ||
| 42 | <<< "str.find(\"fox\", 20):", str.find("fox", 20) >>>; | ||
| 43 | |||
| 44 | // test rfind | ||
| 45 | <<< "str.rfind('o'):", str.rfind('o') >>>; | ||
| 46 | // test rfind | ||
| 47 | <<< "str.rfind('o', 20):", str.rfind('o', 20) >>>; | ||
| 48 | // test rfind | ||
| 49 | <<< "str.find(\"fox\"):", str.find("fox") >>>; | ||
| 50 | // test rfind | ||
| 51 | <<< "str.find(\"fox\", 20):", str.find("fox", 20) >>>; | ||
| 52 | |||
| @@ -0,0 +1,14 @@ | |||
| 1 | // the very hacked StringTokenizer | ||
| 2 | |||
| 3 | // make one | ||
| 4 | StringTokenizer tok; | ||
| 5 | |||
| 6 | // set the string | ||
| 7 | tok.set( "tokenize me please!" ); | ||
| 8 | |||
| 9 | // iterate | ||
| 10 | while( tok.more() ) | ||
| 11 | { | ||
| 12 | |||
| 13 | <<< tok.next(), "" >>>; | ||
| 14 | } | ||
| @@ -0,0 +1,6 @@ | |||
| 1 | // instantiate object | ||
| 2 | Event e; | ||
| 3 | Object o; | ||
| 4 | |||
| 5 | // call | ||
| 6 | <<< e.toString() + " --- " + o.toString() >>>; | ||