String
The String module provides functions for working with UTF-8 encoded text. Strings in Kit are immutable sequences of characters.
Kit supports string interpolation using ${...} syntax.
name = "Alice"
age = 30
println "Hello, ${name}! You are ${age} years old."
# => Hello, Alice! You are 30 years old.
Basics
++ operator is an alias.
println (String.concat "Hello, " "World!")
# => "Hello, World!"
println ("Hello, " ++ "World!")
# => "Hello, World!"
println (String.length "Hello")
# => 5
println (String.repeat "ab" 3)
# => "ababab"
println (String.reverse "Hello")
# => "olleH"
Accessing Characters
Some character at the specified index (0-based) as a single-character string, or None if out of bounds.
println (String.char-at "Hello" 1)
# => Some("e")
println (String.char-at "Hi" 10)
# => None
println (String.substring "Hello, World!" 0 5)
# => "Hello"
substring.
println (String.slice "Hello, World!" 7 12)
# => "World"
Splitting and Joining
println (String.split "a,b,c" ",")
# => ["a", "b", "c"]
println (String.join ", " ["a", "b", "c"])
# => "a, b, c"
unlines for the inverse operation.
println (String.lines "line1\nline2\nline3")
# => ["line1", "line2", "line3"]
println (String.words "hello world foo")
# => ["hello", "world", "foo"]
words.
println (String.split-spaces "a b c")
# => ["a", "b", "c"]
Transforming
println (String.trim " hello ")
# => "hello"
println (String.trim-left " hello ")
# => "hello "
println (String.trim-right " hello ")
# => " hello"
println (String.replace "Hello, World!" "World" "Kit")
# => "Hello, Kit!"
println (String.remove-spaces "h e l l o")
# => "hello"
println (String.capitalize "hello")
# => "Hello"
Searching
true if the string contains the substring.
println (String.contains? "Hello, World!" "World")
# => true
true if the string starts with the prefix.
println (String.starts-with? "Hello, World!" "Hello")
# => true
true if the string ends with the suffix.
println (String.ends-with? "Hello, World!" "!")
# => true
-1 if not found.
println (String.index-of "Hello, World!" "World")
# => 7
println (String.index-of "Hello" "xyz")
# => -1
-1 if not found.
println (String.last-index-of "hello world world" "world")
# => 12
println (String.last-index-of "hello" "xyz")
# => -1
println (String.indices-of "abcabcabc" "abc")
# => [0, 3, 6]
println (String.indices-of "hello" "xyz")
# => []
-1 if no character matches.
# Find first vowel
println (String.find-any "hello" "aeiou")
# => 1
# Find first digit
println (String.find-any "abc123" "0123456789")
# => 3
-1 if no character matches.
# Find last vowel
println (String.find-any-last "hello" "aeiou")
# => 4
# Find last digit
println (String.find-any-last "a1b2c3" "0123456789")
# => 5
println (String.count "abracadabra" "a")
# => 5
Predicates
true if the string has no characters.
println (String.is-empty? "") # => true
println (String.is-empty? "hi") # => false
true if the string is empty or contains only whitespace.
println (String.is-blank? " ") # => true
println (String.is-blank? " hi ") # => false
Case Conversion
println (String.to-upper "hello") # => "HELLO"
println (String.to-lower "HELLO") # => "hello"
println (String.to-kebab "helloWorld")
# => "hello-world"
println (String.to-camel "hello-world")
# => "helloWorld"
println (String.to-snake "helloWorld")
# => "hello_world"
println (String.to-pascal "hello-world")
# => "HelloWorld"
Padding
println (String.pad-left "42" 5 "0")
# => "00042"
println (String.pad-right "hi" 5 ".")
# => "hi..."
Comparison
println (String.eq? "hello" "hello") # => true
println (String.ne? "hello" "world") # => true
println (String.lt? "apple" "banana") # => true
println (String.gt? "zebra" "apple") # => true
-1 if first is less, 0 if equal, 1 if greater.
println (String.compare "apple" "banana") # => -1
println (String.compare "hello" "hello") # => 0
println (String.show "hello")
# => "\"hello\""
s = String.force "hello"
println s
# => "hello"
Bytes and Characters
Char values (Unicode code points).
println (String.chars "hello")
# => ['h', 'e', 'l', 'l', 'o']
# Iterate over characters
"Kit" |> String.chars |> each (fn(c) => println c)
Char values.
println (String.from-chars ['h', 'i'])
# => "hi"
println (String.to-bytes "hi")
# => [104, 105]
println (String.from-bytes [104, 105])
# => "hi"
println (String.byte-at "hi" 0)
# => 104
Char Module
Kit has a Char type representing a single Unicode code point.
Characters are written with single quotes: 'a', 'Z', '\n'.
The Char type represents a single Unicode scalar value (code point).
Use String.chars to convert a string to a list of characters,
and String.from-chars to convert back.
println (Char.to-int 'A')
# => 65
println (Char.to-int 'a')
# => 97
println (Char.from-int 65)
# => 'A'
println (Char.from-int 128512)
# => Unicode emoji character
println (Char.to-string 'K')
# => "K"
Character Classification
true if the character is alphabetic (a-z, A-Z).
println (Char.is-alpha? 'a') # => true
println (Char.is-alpha? '5') # => false
true if the character is a digit (0-9).
println (Char.is-digit? '5') # => true
println (Char.is-digit? 'a') # => false
true if the character is alphabetic or numeric.
println (Char.is-alphanumeric? 'a') # => true
println (Char.is-alphanumeric? '5') # => true
println (Char.is-alphanumeric? '!') # => false
true if the character is whitespace (space, tab, newline, etc.).
println (Char.is-whitespace? ' ') # => true
println (Char.is-whitespace? '\n') # => true
println (Char.is-whitespace? 'a') # => false
true if the character is uppercase.
println (Char.is-upper? 'A') # => true
println (Char.is-upper? 'a') # => false
true if the character is lowercase.
println (Char.is-lower? 'a') # => true
println (Char.is-lower? 'A') # => false
Case Conversion
println (Char.to-upper 'a')
# => 'A'
println (Char.to-lower 'A')
# => 'a'
Built-ins
These functions are available without a module prefix.
Char values (Unicode code points). Equivalent to String.chars.
println (chars "Kit")
# => ['K', 'i', 't']
String.lines.
lines = ["line1", "line2", "line3"]
println (unlines lines)
# => "line1\nline2\nline3"
println (show "hello")
# => "\"hello\""
println (show [1, 2, 3])
# => "[1, 2, 3]"
println (show {name: "Kit", version: 1})
# => "{name: \"Kit\", version: 1}"