| from-rune rune/int -> string | Constructs a single-character string from one Unicode code point. |
|---|---|
| from-runes runes/List -> string | Constructs a string from a list of Unicode code points. |
| format format/string object/any -> string | Formats the object according to the given format. |
| from-utf-16 byte-array/ByteArray -> string | Treats the byte array as little endian UTF-16 and converts it to a string. |
printf.^ for centering.%g or %p.%u type treats integer as unsigned 64 bit integers.
[alignment][precision][type]
alignment = flags<digits>
flags = '-' | '^' | '>' (> is default, can't be used in the syntax)
precision = .<digits>
type 'd' | 'f' | 's' | 'o' | 'x' | 'c' | 'b' | 'u'
str1 := string.from-rune 'a' // -> "a"
str2 := string.from-rune 0x41 // -> "A"
str3 := string.from-rune 42 // -> "*"
str4 := string.from-rune 7931 // -> "☃"
str1 := string.from-runes ['a', 'b', 42] // -> "ab*"
str2 := string.from-runes [0x41] // -> "A"
str3 := string.from-runes [42] // -> "*"
str4 := string.from-runes [7931, 0x20ac] // -> "☃€"
"a".compare-to "b" // => -1
"a".compare-to "a" // => 0
"b".compare-to "a" // => 1
"ab".compare-to "abc" // => -1
"abc".compare-to "ab" // => 1
"Amélie".compare-to "Amelie" // => 1
"Amélie".compare-to "Amzlie" // => 1
compare-to calls.
// In class A with fields str-field1 and str-field2:
compare-to other/A -> int:
return str-field1.compare-to other.str-field1 --if-equal=:
str-field2.compare-to other.str-field2
size.repeat: block.call this[it]
"é".do: print it // 233, null
"Amélie".do --runes: print "$(%c it)" // A, m, é, l, i, e
str.to-byte-array.map instead.
heavy-metalize str/string -> string:
return str.flat_map: | c |
{'o': 'ö', 'a': 'ä', 'u': 'ü', 'ä': "\u{20db}a"}.get c --if-absent=: c
lower-case str/string -> string:
return str.flat-map: | c | ('A' <= c <= 'Z') ? c - 'A' + 'a' : c
"Toad".glob "Toad" // => true
"Toad".glob "To?d" // => true
"Toad".glob "To" // => false
"To*d".glob "To\\*d" // => true
"Toad".glob "To\\*d" // => false
O(1).
"foobar".index-of "foo" // => 0
"foobar".index-of "bar" // => 3
"foo".index-of "bar" // => -1
"foobarfoo".index-of "foo" // => 0
"foobarfoo".index-of "foo" 1 // => 6
"foobarfoo".index-of "foo" 1 8 // => -1
// Invalid ranges:
"foobarfoo".index-of "foo" -1 999 // Throws.
"foobarfoo".index-of "foo" 1 999 // Throws.
"".index-of "" 0 0 // => 0
"".index-of "" -3 -3 // => Throws
"".index-of "" 2 2 // => Throws
// Last:
"foobarfoo".index-of --last "foo" // => 6
"foobarfoo".index-of --last "foo" 1 // => 6
"foobarfoo".index-of --last "foo" 1 6 // => 0
"foobarfoo".index-of --last "foo" 0 1 // => 0
"foobarfoo".index-of --last "foo" 0 8 // => 0
"foobarfoo".index-of --last "gee" // => -1
"foobarfoo".index-of --last "foo" 1 5 // => -1
"foobarfoo".index-of --last "foo" 0 8 // => 0
"foo".index_of "bar" --if-absent=: it.size // => 3 (the size of "foo")
"foobarfoo".index_of "foo" 1 8 --if-absent=: 499 // => 499
"".index_of "" -3 -3 --if-absent=: throw "not found" // Error
"".index_of "" 2 2 --if-absent=: -1 // => -1
"foobarfoo".index_of "foo" 1 8 --if-absent=: 42 // => 42
"Toad the Wet Sprocket".matches "Toad" --at=0 // => true
"Toad the Wet Sprocket".matches "Toad" --at=-1 // => false
"Toad the Wet Sprocket".matches "Sprocket" --at=13 // => true
str[from..to]. Since both arguments are optional (as they have default values), it is valid to omit from or to.
str := "Hello, world!"
hello := str[..5]
world := str[7..]
comma := str[5..6]
print hello // => "Hello"
print comma // => ","
print world // => "world!"
amelie := "Amélie"
amelie[2..3] // Throws an exception.
str := "Amélie"
print "$(%c str[2])" // => é
print str[3] // => null
print "$(%c str[4])" // => l
str := "foo"
str.pad --left 5 // => " foo"
str.pad --left 5 '0' // => "00foo"
str.pad --left 3 // => "foo"
str.pad --left 1 // => "foo"
str.pad 5 // => " foo"
str.pad 5 '0' // => "00foo"
str.pad 3 // => "foo"
str.pad 1 // => "foo"
str := "foo"
str.pad --right 5 // => "foo "
str.pad --right 5 '0' // => "foo00"
str.pad --right 3 // => "foo"
str.pad --right 1 // => "foo"
str := "foo"
str.pad --center 5 // => " foo "
str.pad --center 5 '0' // => "0foo0"
str.pad --center 6 // => " foo "
str.pad --center 6 '0' // => "0foo00"
str.pad --center 3 // => "foo"
str.pad --center 1 // => "foo"
0 <= index <= size.size --runes of 6.
"Toad the Wet Sprocket".split "e": print it // prints "Toad th", " W", "t Sprock", and "t"
" the dust ".split " ": print it // prints "the", "dust", and ""
"abc".split "": print it // prints "a", "b", and "c"
"foo".split "foo": print it // prints "" and ""
"afoo".split "foo": print it // prints "a" and ""
"foob".split "foo": print it // prints "" and "b"
"".split "": print it // Doesn't print.
gadsby := "If youth, throughout all history, had had a champion to stand up for it;"
gadsby.split "e": print it // prints the contents of gadsby
"Toad the Wet Sprocket".split --at-first "e": print it // prints "Toad th", " Wet Sprocket"
" the dust ".split --at-first " ": print it // prints "", "the dust "
gadsby.split --at-first "e": print it // prints the contents of gadsby
"abc".split --at-first "": print it // prints "a" and "bc"
"foo".split --at-first "foo": print it // prints "" and ""
"afoo".split --at-first "foo": print it // prints "a" and ""
"foob".split --at-first "foo": print it // prints "" and "b"
"".split --at-first "": print it // This is an error.
"a".split --at-first "": print it // prints "a" and ""
"foo".split "foo" --drop-empty: print it // Doesn't print.
"afoo".split "foo" --drop-empty: print it // prints "a"
"Toad the Wet Sprocket".split "e" // => ["Toad th", " W", "t Sprock", "t"]
" the dust ".split " " // => ["", "the", "dust", ""]
"abc".split "" // => ["", "a", "b", "c"]
"foo".split "foo" // => ["", ""]
"afoo".split "foo" // => ["a", ""]
"foob".split "foo" // => ["", "b"]
"".split "" // => [""]
gadsby := "If youth, throughout all history, had had a champion to stand up for it;"
gadsby.split "e" // => [gadsby]
"Toad the Wet Sprocket".split --at-first "e" // => ["Toad th", " Wet Sprocket"]
" the dust ".split --at-first " " // => ["", "the dust "]
gadsby.split --at-first "e" // => [gadsby]
"abc".split --at-first "" // => ["", "abc"]
"foo".split --at-first "foo" // => ["", ""]
"afoo".split --at-first "foo" // => ["a", ""]
"foob".split --at-first "foo" // => ["", "b"]
"".split --at-first "" // => [""]
{{variable}}.
"foo {{bar}} baz".substitute: "-0-" // => "foo -0- baz"
"foo {{16}} baz".substitute: (int.parse it) + 1 // => "foo 17 baz"
"x {{ y }} z".substitute: null // => "x {{ y }} z"
"f [hest] b".substitute --open="[" --close="]": "horse" // => "f horse b"
"x {{b}} z".substitute: { "a": "hund", "b": "kat" }[it] // => "x kat z"
0009..000D ; White_Space # Cc <control-0009>..<control-000D>
0020 ; White_Space # Zs SPACE
0085 ; White_Space # Cc <control-0085>
00A0 ; White_Space # Zs NO-BREAK SPACE
1680 ; White_Space # Zs OGHAM SPACE MARK
2000..200A ; White_Space # Zs EN QUAD..HAIR SPACE
2028 ; White_Space # Zl LINE SEPARATOR
2029 ; White_Space # Zp PARAGRAPH SEPARATOR
202F ; White_Space # Zs NARROW NO-BREAK SPACE
205F ; White_Space # Zs MEDIUM MATHEMATICAL SPACE
3000 ; White_Space # Zs IDEOGRAPHIC SPACE
FEFF ; BOM ZERO WIDTH NO_BREAK SPACE
"http://www.example.com".trim --left "http://" // => "www.example.com"
str := "foobar"
str.trim --left "foo" // => "bar"
str.trim --left "bar" // => "foobar"
str.trim --left "gee" // => "foobar"
"https://www.example.com".trim --left "http://" --if-absent=: it.trim --left "https://" // => "www.example.com"
str := "foobar"
str.trim --left "foo" --if-absent=: "not_used" // => "bar"
str.trim --left "" --if-absent=: "not_used" // => "foobar"
str.trim --left "gee" --if-absent=: it // => "foobar" (the default behavior)
str.trim --left "gee" --if-absent=: throw "missing prefix" // ERROR
"hello.toit".trim --right ".toit" // => "hello"
str := "foobar"
str.trim --right "bar" // => "foo"
str.trim --right "foo" // => "foobar"
str.trim --right "gee" // => "foobar"
str := "foobar"
str.trim --right "bar" --if-absent=: "not_used" // => "bar"
str.trim --right "" --if-absent=: "not_used" // => "foobar"
str.trim --right "gee" --if-absent=: it // => "foobar" (the default behavior)
str.trim --right "gee" --if-absent=: throw "missing suffix" // ERROR