D Programming - Characters



Characters are the building blocks of strings. Any symbol of a writing system is called a character: letters of alphabets, numerals, punctuation marks, the space character, etc. Confusingly, the building blocks of characters themselves are called characters as well.

The integer value of the lowercase a is 97 and the integer value of the numeral 1 is 49. These values have been assigned merely by conventions when the ASCII table has been designed.

The following table mentions standard character types with their storage sizes and purposes.

The characters are represented by the char type, which can hold only 256 distinct values. If you are familiar with the char type from other languages, you may already know that it is not large enough to support the symbols of many writing systems.

Type Storage size Purpose
char 1 byte UTF-8 code unit
wchar 2 bytes UTF-16 code unit
dchar 4 bytes UTF-32 code unit and Unicode code point

Some useful character functions are listed below −

  • isLower − Determines if a lowercase character?

  • isUpper − Determines if an uppercase character?

  • isAlpha − Determines if a Unicode alphanumeric character (generally, a letter or a numeral)?

  • isWhite − Determines if a whitespace character?

  • toLower − It produces the lowercase of the given character.

  • toUpper − It produces the uppercase of the given character.

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('ı')); 
}

When the above code is compiled and executed, it produces the following result −

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 

Reading Characters in D

We can read characters using readf as shown below.

readf(" %s", &letter);

Since D programming support unicode, in order to read unicode characters, we need to read twice and write twice to get the expected result. This does not work on the online compiler. The example is shown below.

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); 
} 

When the above code is compiled and executed, it produces the following result −

Please enter a letter: ğ 
The letter that has been read: ğ
Advertisements