How do I increment a character and compare two characters in EK9?
← Operators and Expressions · Ref: Q1192
EK9 Character supports increment (++), decrement (--), comparison, and all standard operators.
CHARACTER LITERALS
letter <- 'a'
INCREMENT
letter++ moves from 'a' to 'b' letter++ moves from 'b' to 'c' letter++ moves from 'c' to 'd'
COMPARISON
if letter == 'd' exact match if letter > 'a' ordering (alphabetical) ordering <- 'a' <=> 'z' spaceship comparison
STRING CONVERSION
str <- $letter character to string
Character uses the same operators as Integer, Date, Money — the pattern is universal across all EK9 types.
See Q238 for the complete operator set. See Q23 for basic types.
Example
defines module qa.operators.characterops defines function <?- Compare two characters and return spaceship ordering. -?> compareChars() as pure -> left as Character right as Character <- rtn as Integer: left <=> right <?- Check if two characters are equal. -?> charsEqual() as pure -> left as Character right as Character <- rtn as Boolean: left == right defines program CharacterOpsDemo() stdout <- Stdout() // === CHARACTER LITERAL === letter <- 'a' stdout.println(`Starting letter: ${letter}`) // === INCREMENT === letter++ stdout.println(`After first ++: ${letter}`) letter++ stdout.println(`After second ++: ${letter}`) letter++ stdout.println(`After third ++: ${letter}`) // === COMPARISON VIA FUNCTION === targetLetter <- 'd' matched <- charsEqual(letter, targetLetter) stdout.println(`Letter equals target: ${matched}`) // === SPACESHIP ORDERING VIA FUNCTION === ordering <- compareChars(letter, targetLetter) stdout.println(`letter <=> target: ${ordering}`) alphaOrder <- compareChars('a', 'z') stdout.println(`'a' <=> 'z': ${alphaOrder}`) // === DECREMENT === letter-- stdout.println(`After --: ${letter}`) // === STRING CONVERSION === charStr <- $letter stdout.println(`Character as string: ${charStr}`) // === ISSET === require letter? unsetChar <- Character() require ~unsetChar? // === SORTED CHARACTER LIST === chars <- List() of Character chars += 'z' chars += 'a' chars += 'm' sorted <- cat chars | sort | collect as List of Character stdout.println(`Sorted chars: ${sorted}`)
Common mistakes
E07620 — Character does not support addition with Integer. Use the ++ operator to increment a Character to the next Unicode code point. See ek9 -h E01072 for details.
Incorrect:
letter = letter + 1
Correct:
letter++
Other ways to ask this
- Increment a Character from 'a' to 'd' using ++ in EK9.
- I need to iterate through characters by incrementing a letter variable.
- In C I used char++ to walk through the alphabet — does EK9 support this?
Coming from another language?
Java: char is a primitive integer type, char++ works. Python: no char type, ord()/chr() functions. Rust: char is Unicode scalar, no increment. Go: rune is int32, rune++ works. EK9: Character is a full type with ++, --, comparison, $, ? operators.
Keywords: increment, char, walk, character, operator, alphabet, compare, letter, decrement