W3Cschool
恭喜您成為首批注冊(cè)用戶(hù)
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
字符由char類(lèi)型表示,只能容納256個(gè)不同的值。
類(lèi)型 | 存儲(chǔ)空間 | 用途 |
---|---|---|
char | 1個(gè)字節(jié) | UTF-8代碼單元 |
wchar | 2個(gè)字節(jié) | UTF-16代碼單元 |
dchar | 4個(gè)字節(jié) | UTF-32代碼單元和Unicode代碼點(diǎn) |
下面列出了一些有用的字符函數(shù)-
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('\n'));
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('?'));
}
編譯并執(zhí)行上述代碼后,將產(chǎn)生以下輸出-
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);
由于D編程支持unicode,因此為了讀取unicode字符,我們需要讀取兩次并寫(xiě)入兩次才能獲得預(yù)期的輸出。該如下所示。
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);
}
編譯并執(zhí)行上述代碼后,將產(chǎn)生以下輸出-
Please enter a letter: ?
The letter that has been read: ?
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話(huà):173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: