ERights Home elang / collect 
No Previous Sibling On to: Using Tables

Collection Type:

E's Strings are ConstLists (immutable sequences) of unicode characters. Here are their operators in precedence order:

Boolean Comparisons (non-associative)

Example Meaning Expansion Value
"foo" == "bar" Are they the same? __equalizer.sameEver("foo", "bar") false
"foo" != "bar" Are they different? __equalizer.sameEver("foo", "bar").not() true

As you'd expect.

Magnitude Comparisons (non-associative)

Example Meaning Expansion Value
"foo" < "bar"
less than
"foo" compareTo("bar") belowZero
"foo" <= "bar"
less or equal to
"foo" compareTo("bar") atMostZero
"foo" >= "bar"
greater or equal to
"foo" compareTo("bar") atLeastZero
"foo" > "bar"
greater than
"foo" compareTo("bar") aboveZero
"foo" <=> "bar"
as big as
"foo" compareTo("bar") isZero

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)

Example Meaning Expansion Value
"foo" + "bar" addition "foo" add("bar") "foobar"

As in Java, Strings add by concatenating.

Multiplicative Expressions (left associative)

Example Meaning Expansion Value
"foo" * 3
"foo" multiply(3)

If you multiply a String by an integer, it's as if you added it to itself that many times.

Indexing Expression (left associative)

Example Meaning Expansion Value
get char by position
"foobar" get(3)
"foobar"(2, 4)
get substring by positions
"foobar" run(2,4)
get substring by interval
"foobar" run(2..!4)

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 Messages

E'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 Messages

In addition to the above operators and messages, Strings respond to the following messages:

? "foobar".replaceAll("oo", "u")
# value: "fubar"
"replaceAll(..)" is derived from java.lang.String's "replace", but takes strings as arguments rather than characters.
? 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.
ERights Home elang / collect 
No Previous Sibling On to: Using Tables
Download    FAQ    API    Mail Archive    Donate

report bug (including invalid html)

Golden Key Campaign Blue Ribbon Campaign