Char
The Char type represents a single Unicode character (code point). Characters are enclosed in single quotes and support Unicode.
Literals
Character Literal
Char
Character literals are enclosed in single quotes.
letter = 'a'
digit = '5'
emoji = '🦊'
println letter
# => 'a'
Conversions
Char.from-code-point
Int -> Option Char
Creates a character from a Unicode code point. Returns
None for invalid code points.
println (Char.from-code-point 65)
# => Some('A')
println (Char.from-code-point 128054)
# => Some('🐶')
println (Char.from-code-point -1)
# => None
Char.to-code-point
Char -> Int
Returns the Unicode code point of a character.
println (Char.to-code-point 'A')
# => 65
println (Char.to-code-point '🐶')
# => 128054
Char.from-string
String -> Option Char
Converts a single-character string to a Char. Returns
None if the string is empty or has multiple characters.
println (Char.from-string "a")
# => Some('a')
println (Char.from-string "ab")
# => None
println (Char.from-string "")
# => None
Char.to-string
Char -> String
Converts a character to a single-character string.
println (Char.to-string 'a')
# => "a"
println (Char.to-string '🦊')
# => "🦊"
Predicates
Char.is-alpha?
Char -> Bool
Returns
true if the character is an alphabetic letter (a-z, A-Z).
println (Char.is-alpha? 'a') # => true
println (Char.is-alpha? 'Z') # => true
println (Char.is-alpha? '5') # => false
Char.is-digit?
Char -> Bool
Returns
true if the character is a decimal digit (0-9).
println (Char.is-digit? '5') # => true
println (Char.is-digit? 'a') # => false
Char.is-alphanumeric?
Char -> Bool
Returns
true if the character is alphabetic or a digit.
println (Char.is-alphanumeric? 'a') # => true
println (Char.is-alphanumeric? '5') # => true
println (Char.is-alphanumeric? '!') # => false
Char.is-whitespace?
Char -> Bool
Returns
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
Char.is-uppercase?
Char -> Bool
Returns
true if the character is an uppercase letter.
println (Char.is-uppercase? 'A') # => true
println (Char.is-uppercase? 'a') # => false
Char.is-lowercase?
Char -> Bool
Returns
true if the character is a lowercase letter.
println (Char.is-lowercase? 'a') # => true
println (Char.is-lowercase? 'A') # => false
Char.is-ascii?
Char -> Bool
Returns
true if the character is in the ASCII range (0-127).
println (Char.is-ascii? 'a') # => true
println (Char.is-ascii? '🦊') # => false
Char.is-control?
Char -> Bool
Returns
true if the character is a control character.
println (Char.is-control? '\n') # => true
println (Char.is-control? 'a') # => false
Char.is-punctuation?
Char -> Bool
Returns
true if the character is a punctuation character.
println (Char.is-punctuation? '.') # => true
println (Char.is-punctuation? '!') # => true
println (Char.is-punctuation? 'a') # => false
Char.is-hex-digit?
Char -> Bool
Returns
true if the character is a hexadecimal digit (0-9, a-f, A-F).
println (Char.is-hex-digit? 'a') # => true
println (Char.is-hex-digit? 'F') # => true
println (Char.is-hex-digit? 'g') # => false
Char.is-printable?
Char -> Bool
Returns
true if the character is printable (visible when rendered).
println (Char.is-printable? 'a') # => true
println (Char.is-printable? ' ') # => true
println (Char.is-printable? '\n') # => false
Case Conversion
Char.to-uppercase
Char -> Char
Converts a character to uppercase. Non-letter characters are returned unchanged.
println (Char.to-uppercase 'a') # => 'A'
println (Char.to-uppercase 'A') # => 'A'
println (Char.to-uppercase '5') # => '5'
Char.to-lowercase
Char -> Char
Converts a character to lowercase. Non-letter characters are returned unchanged.
println (Char.to-lowercase 'A') # => 'a'
println (Char.to-lowercase 'a') # => 'a'
println (Char.to-lowercase '5') # => '5'
Comparison
Char.compare
Char -> Char -> Int
Compares two characters by their code points. Returns
-1 if less, 0 if equal, 1 if greater.
println (Char.compare 'a' 'b') # => -1
println (Char.compare 'b' 'b') # => 0
println (Char.compare 'c' 'a') # => 1
Working with Strings
Use String.chars to convert a string to a list of characters,
and String.from-chars to convert back.
chars = String.chars "hello"
# => ['h', 'e', 'l', 'l', 'o']
upper = chars |> map Char.to-uppercase |> String.from-chars
# => "HELLO"