E's Strings are ConstLists (immutable sequences) of unicode characters. Here are their operators in precedence order: Boolean Comparisons (non-associative)
As you'd expect. Magnitude Comparisons (non-associative)
If you ask the Strings to compare themselves, they compare according to their unicode character codes. The "<=>" operator is E's way of asking whether two things have the same magnitude (are equivalent for sorting purposes). For Strings, "<=>" always gives the same answer as "==". For locale-specific sorting orders, see ***. Additive Expressions (left associative)
As in Java, Strings add by concatenating. Multiplicative Expressions (left associative)
If you multiply a String by an integer, it's as if you added it to itself that many times. Indexing Expression (left associative)
You index into a string to get the character at that position in the String. The first position in a String is numbered zero, so position number 3 of "foobar" is "foobar"'s fourth character, a 'b'. You can extract a run of a string by using parens instead of square brackets. As with Java's String's substring message, the first number is the index of the beginning of the run to extract, and the second number is the index immediately after the last character to extract. If you use an interval instead of a pair of numbers, then the characters are indices that are in this interval as extracted in order to make the returned substring. Since Strings are immutable, one cannot use these expressions to assign into a String.
Strings respond to ConstList messages? pragma.syntax("0.8") ? "foobar".size() # value: 6 As you'd expect. Being Lists, Strings also support iteration: ? for i => c in "foo" { > def code := c.asInteger() > println(`$i => $c ($code)`) > } # stdout: 0 => f (102) # 1 => o (111) # 2 => o (111) # ? "foobar".indexOf1('o') # value: 1 ? "foobar".startOf("oo") # value: 1 ? "foobar".startOf("ob") # value: 2 ? "foobar".lastIndexOf1('o') # value: 2
java.lang.String MessagesE's Strings are actually Java Strings, of the messages defined in the javadoc-umentation for java.lang.String the following messages are useful from E. ? "foobar".startsWith("bar") # value: false ? "foobar".endsWith("bar") # value: true ? "foo".equalsIgnoreCase("FoO") # value: true ? "FooBar".toLowerCase() # value: "foobar" ? "FooBar".toUpperCase() # value: "FOOBAR" ? def str := " \n\t foobar\n" # value: " # \t foobar # " ? str.size() # value: 13 ? def trimmed := str.trim() # value: "foobar" ? trimmed.size() # value: 6 trim() returns a String with the whitespace removed from both ends.
Additional String MessagesIn addition to the above operators and messages, Strings respond to the following messages: "replaceAll(..)" is derived from java.lang.String's "replace", but takes strings as arguments rather than characters.? "foobar".replaceAll("oo", "u") # value: "fubar" ? for line in str.split("\n") { > println(line.size()) > } # stdout: 2 # 9 # 0 # "split(separator)" breaks a String up into segments separated by occurrences of the separator, and returns an array containing each segment. Each segment in the array contains only the text between the separators, not the separators themselves. Since the original string ended with a newline, the zero length segment above represents the segment between this last separator occurrence and the end of the string. This operation was inspired by Python's string split operation. (*** document the inverse: rjoin) |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Unless stated otherwise, all text on this page which is either unattributed or by Mark S. Miller is hereby placed in the public domain.
|