字符是字符串的基石。文字系统的任何符号称为字符:字母,数字,标点符号,空格字符等字母令人困惑的是,字符本身的基石也被称为字符。
小写a的整数值是97,数字1的整数值是49,这些数值已经仅仅指派当ASCII码表的设计惯例。
下表提供了有关与存储大小,其目的标准字符类型的详细信息。
字符由char类型,它只能容纳256个不同的值来表示。如果熟悉其他语言的字符类型,可能已经知道这不是大到足以支持许多书写系统的符号。
类型 | 存储大小 | 目的 |
---|---|---|
char | 1 byte | UTF-8 code unit |
wchar | 2 bytes | UTF-16 code unit |
dchar | 4 bytes | UTF-32 code unit and Unicode code web3 |
一些有用的字符函数列表如下
isLower:是小写字符?
isUpper:是否为大写字母?
isAlpha: 是一个Unicode字母数字字符(通常,一个字母或数字)?
isWhite:是一个空白字符?
toLower: 给定字符转为小写
toUpper: 给定字符转为大写
import std.stdio; |
import std.uni; |
void main() |
{ |
writeln("Is ğ lowercase? ", isLower('ğ')); |
writeln("Is Ş lowercase? ", isLower('Ş')); |
writeln("Is İ uppercase? ", isUpper('İ')); |
writeln("Is ç uppercase? ", isUpper('ç')); |
writeln("Is z alphanumeric? ", isAlpha('z')); |
writeln("Is new-line whitespace? ", isWhite(' |
')); |
writeln("Is underline whitespace? ", isWhite('_')); |
writeln("The lowercase of Ğ: ", toLower('Ğ')); |
writeln("The lowercase of İ: ", toLower('İ')); |
writeln("The uppercase of ş: ", toUpper('ş')); |
writeln("The uppercase of ı: ", toUpper('ı')); |
} |
Is ğ lowercase? true |
Is Ş lowercase? false |
Is İ uppercase? true |
Is ç uppercase? false |
Is z alphanumeric? true |
Is new-line whitespace? true |
Is underline whitespace? false |
The lowercase of Ğ: ğ |
The lowercase of İ: i |
The uppercase of ş: Ş |
The uppercase of ı: I |
读取字符
我们可以用readf如下所示读取字符。
readf(" %s", &letter); |
import std.stdio; |
void main() |
{ |
char firstCode; |
char secondCode; |
write("Please enter a letter: "); |
readf(" %s", &firstCode); |
readf(" %s", &secondCode); |
writeln("The letter that has been read: ", |
firstCode, secondCode); |
} |
当我们运行上面的程序,我们会得到下面的输出
Please enter a letter: ğ The letter that has been read: ğ