.\" Made by website/plain/build.mjs from website/words/char.md; change that file. .TH SG-CHAR 7 2026-11-28 "SG" "SG reference" .SH NAME char \- one character of Unicode, in 32 bits; not a number .SH SYNOPSIS .TP \fB\&'a', 'é', '🐈'\fR a character, in single quotes .TP \fB\&'\en', '\et', '\e'', '\eu{1F600}'\fR an escape, or the character of a code in hex .TP \fBtext.chars()\fR the characters of a text, one at a time .TP \fB(int)c, (char)n, n.tryToChar()\fR the code of a character, the character of a code, or null when no character has it .TP \fBc.isAsciiDigit(), c.isLetter(), c.toUpper()\fR tests and changes of case .TP \fBtext.write(c), c.toString(), $"{c}"\fR a character into a text .TP \fB\&'a'..='z' => ...\fR in a match, an arm for a range of characters .SH DESCRIPTION A \fBchar\fR is one character of Unicode, any code from 0 to 0x10FFFF but the ones from 0xD800 to 0xDFFF, kept in 32 bits. An emoji is one \fBchar\fR\&. It is written in single quotes, which hold exactly one character, and takes the escapes of a text: \fB\en\fR, \fB\et\fR, \fB\er\fR, \fB\e0\fR, \fB\e\e\fR, \fB\e"\fR, \fB\e'\fR and \fB\eu{...}\fR with one to six hex digits. .PP A \fBchar\fR is not a number. It has no arithmetic and is never compared with an integer; \fB(int)c\fR gives its code and \fB(char)n\fR the character of a code. \fB(char)n\fR stops the program when no character has the code, and \fBn.tryToChar()\fR gives \fBnull\fR there. Two characters compare by their codes, so \fB\&'a' < 'b'\fR\&. .PP A text is UTF-8, so a \fBchar\fR takes one to four bytes in it. \fBtext[i]\fR reads a byte, not a character; \fBchars()\fR reads the text character by character. A character constant of ASCII is a \fBbyte\fR where a \fBbyte\fR goes, as in \fBtext[i] == ':'\fR; anywhere else it is a \fBchar\fR, and \fBvar c = 'a'\fR is a \fBchar\fR\&. .PP The tests of ASCII (\fBisAsciiDigit\fR, \fBisAsciiLetter\fR ...) answer the same for every script. The tests of Unicode (\fBisLetter\fR, \fBisNumeric\fR, \fBisUpper\fR ...) and \fBtoUpper\fR and \fBtoLower\fR follow the tables of Unicode 17.0, one character for one. The methods are in Text. .SH EXAMPLES .SS "A character and its code" .RS 4 .nf void main() { var letter = 'é' var smile = '\eu{1F600}' print($"{letter} {(int)letter} {letter.utf8Length()}") print($"{smile} U+{(int)smile:X} {smile.utf8Length()}") print((char)65) print('\et' == (char)9) print('a' < 'b') } .fi .RE .PP .RS 4 .nf é 233 2 😀 U+1F600 4 A true true .fi .RE .PP \fB(int)letter\fR is the code of \fBé\fR; \fButf8Length\fR counts the bytes it takes in a text. .SS "The characters of a text" .RS 4 .nf void main() { var name = "Paşa 🐈" print($"{name.length()} bytes") nint count = 0 for _ in name.chars() { count += 1 } print($"{count} characters") print(name[2]) var letters = name.chars() _ = letters.next() _ = letters.next() print(letters.next() ?? '?') } .fi .RE .PP .RS 4 .nf 10 bytes 6 characters 197 ş .fi .RE .PP \fBş\fR takes two bytes and the cat four. \fBname[2]\fR is the first byte of \fBş\fR, a number; the third \fBchar\fR is \fBş\fR itself. .SS "Tests and case" .RS 4 .nf void main() { print('7'.isAsciiDigit()) print('٣'.isAsciiDigit()) print('٣'.isNumeric()) print('Ä'.isLetter() && 'Ä'.isUpper()) print('ä'.toUpper()) print('ß'.toUpper()) print('€'.category()) print('\eu{a0}'.isWhitespace()) } .fi .RE .PP .RS 4 .nf true false true true Ä ß CurrencySymbol true .fi .RE .PP \fB٣\fR is the Arabic-Indic digit three: a number to Unicode, not a digit \fBparse\fR reads. \fBß\fR has no single character in upper case, so \fBtoUpper\fR leaves it. .SS "Into a text" .RS 4 .nf void main() { var line = string.withCapacity(16) for c in "dog".chars() { line.write(c.toUpper()) line.write('-') } line.write('🐈') print(line) string one = 'é'.toString() print($"{one.length()} [{'x',3}] [{'x',-3}]") print("a,b;c".split(';').next() ?? "") print("naïve".contains('ï')) print("naïve".indexOf('v') ?? -1) } .fi .RE .PP .RS 4 .nf D-O-G-🐈 2 [ x] [x ] a,b true 4 .fi .RE .PP \fBwrite\fR puts the bytes of the character at the end of the text. \fBsplit\fR, \fBcontains\fR and \fBindexOf\fR take a \fBchar\fR; the place \fBindexOf\fR gives is in bytes. .SS "In a match" .RS 4 .nf string kind(char c) { return match c { 'a'..='z' or 'A'..='Z' => "letter" '0'..='9' => "digit" ' ' or '\et' => "space" _ => "other" } } void main() { for c in "a1 é!".chars() { print($"{c} {kind(c)}") } } .fi .RE .PP .RS 4 .nf a letter 1 digit space é other ! other .fi .RE .PP \fB\&..=\fR takes both ends. \fBé\fR is a letter, but not one of the ranges. .SS "ROT13, through the code" .RS 4 .nf char rot13(char c) { return match c { 'a'..='z' => (char)(((int)c - 'a' + 13) % 26 + 'a') 'A'..='Z' => (char)(((int)c - 'A' + 13) % 26 + 'A') _ => c } } void main() { var secret = string.withCapacity(32) for c in "Hello, Paşa!".chars() { secret.write(rot13(c)) } print(secret) } .fi .RE .PP .RS 4 .nf Uryyb, Cnşn! .fi .RE .PP The arithmetic is on the code, an \fBint\fR; \fB\&'a'\fR beside an \fBint\fR is a number. \fB(char)\fR turns the code back into a character, and cannot fail here: the code is always one of a letter. .SS "A slug for an address" .RS 4 .nf string slug(read string title) { var out = string.withCapacity(title.length()) bool gap = false for c in title.chars() { if c.isAlphanumeric() { if gap && out.length() > 0 { out.write('-') } out.write(c.toLower()) gap = false } else { gap = true } } return out } void main() { print(slug("Hello, Wörld!")) print(slug(" SG 0.1: Bytes & Characters ")) } .fi .RE .PP .RS 4 .nf hello-wörld sg-0-1-bytes-characters .fi .RE .PP Every run of characters that are neither letters nor digits becomes one \fB\-\fR\&. \fBö\fR is a letter, so it stays. .SS "Codes that may be no character" .RS 4 .nf void main() { var codes = List { 0x48, 0xe9, 0xD800, 0x1F408, 0x110000 } for n in codes { match n.tryToChar() { null => print($"{n:X}: no character") var c => print($"{n:X}: {c}") } } } .fi .RE .PP .RS 4 .nf 48: H E9: é D800: no character 1F408: 🐈 110000: no character .fi .RE .PP 0xD800 to 0xDFFF are kept for UTF-16, and no code is past 0x10FFFF. .SH "WHAT STOPS THE PROGRAM" .SS "A code that is no character" .RS 4 .nf void main() { var codes = List { 0x41, 0xD800 } for n in codes { print((char)n) } } .fi .RE .PP .RS 4 .nf A panic: 0xD800 is not the code of a character --> example.sg:4:15 4 | print((char)n) | ^^^^^^^ hint: `tryToChar()` gives `null` when no character has the code .fi .RE .PP This is checked in every build, a release build too: a bad code written into a text would make it UTF-8 that is not valid. .SS "A character that is not a byte" .RS 4 .nf void main() { var sign = '€' byte b = (byte)sign print(b) } .fi .RE .PP .RS 4 .nf panic: value 8364 does not fit in byte --> example.sg:3:14 3 | byte b = (byte)sign | ^^^^^^^^^^ hint: `(int)` in place of `(byte)` holds the code of every character .fi .RE .PP \fB(byte)c\fR gives the code, not the bytes of UTF-8, and follows the rule of the integers: a release build keeps the low 8 bits. The bytes are \fBc.toString().asBytes()\fR\&. .SH "WHAT THE COMPILER REFUSES" .SS "Two characters in single quotes" .RS 4 .nf void main() { var pair = 'ab' print(pair) } .fi .RE .PP .RS 4 .nf error: single quotes hold exactly one character --> example.sg:2:16 2 | var pair = 'ab' | ^^^^ hint: text is written with double quotes: `"ab"` .fi .RE .PP \fB\&''\fR is refused too: \fBthere is no character between the quotes\fR\&. .SS "A char where a string goes" .RS 4 .nf void main() { string name = 'P' print(name) } .fi .RE .PP .RS 4 .nf error: expected `string`, found `char` --> example.sg:2:19 2 | string name = 'P' | ^^^ hint: a text is written in double quotes: `"P"` .fi .RE .PP \fB"P"\fR is the same text, written in the program. .SS "Arithmetic" .RS 4 .nf void main() { var letter = 'a' print(letter + 1) } .fi .RE .PP .RS 4 .nf error: operator `+` is not defined for `char` and `int` a `char` is not a number: `(int)c` is its code, and `(char)n` the character of a code --> example.sg:3:11 3 | print(letter + 1) | ^^^^^^^^^^ .fi .RE .SS "A char where an int goes" .RS 4 .nf void main() { var letter = 'a' int code = letter print(code) } .fi .RE .PP .RS 4 .nf error: expected `int`, found `char` --> example.sg:3:16 3 | int code = letter | ^^^^^^ hint: the code of the character: `(int)letter` .fi .RE .PP Comparing with a number, \fBletter == 97\fR, is refused the same way: \fB(int)letter == 97\fR compares the code. .SS "A byte where a char goes" .RS 4 .nf void main() { var text = "abc" char first = text[0] print(first) } .fi .RE .PP .RS 4 .nf error: expected `char`, found `byte` --> example.sg:3:18 3 | char first = text[0] | ^^^^^^^ hint: a byte of a text is a character only below 128, as UTF-8 writes the others in more bytes; the characters are `text.chars()` .fi .RE .PP \fBtext[0]\fR is a byte of UTF-8, which is a character alone only in ASCII. The first character is \fBtext.chars().next()\fR\&. .SS "A char beside a text" .RS 4 .nf void main() { for c in "café".chars() { if c == "é" { print("found") } } } .fi .RE .PP .RS 4 .nf error: operator `==` is not defined for `char` and `string` --> example.sg:3:12 3 | if c == "é" { | ^^^^^^^^ .fi .RE .PP A character is written in single quotes: \fBc == 'é'\fR\&. .SS "isDigit" .RS 4 .nf void main() { var c = '7' print(c.isDigit()) } .fi .RE .PP .RS 4 .nf error: type `char` has no method `isDigit` `isNumeric()` says yes to the digits of every script, and to `'½'` and `'²'`, which `parse` does not read --> example.sg:3:13 3 | print(c.isDigit()) | ^^^^^^^ hint: `isAsciiDigit()`: the digits `parse` reads .fi .RE .SS "A code known to be no character" .RS 4 .nf void main() { var c = (char)0xD800 print(c) } .fi .RE .PP .RS 4 .nf error: `0xD800` is not the code of a character a character is from 0 to 0x10FFFF, and not from 0xD800 to 0xDFFF --> example.sg:2:13 2 | var c = (char)0xD800 | ^^^^^^^^^^^^ .fi .RE .PP \fB\&'\eu{110000}'\fR is refused the same way, and \fB\&'\ex41'\fR as an escape it does not know: a code is written \fB\&'\eu{41}'\fR\&. .SH "SEE ALSO" .BR sg-string (7), .BR sg-byte (7), .BR sg-match (7), .BR sg-int (7) .PP https://sg\-lang.org/reference/char.html