D Programming - Strings



D provides following two types of string representations −

  • Character array
  • Core language string

Character Array

We can represent the character array in one of the two forms as shown below. The first form provides the size directly and the second form uses the dup method which creates a writable copy of the string "Good morning".

char[9]  greeting1 = "Hello all"; 
char[] greeting2 = "Good morning".dup; 

Example

Here is a simple example using the above simple character array forms.

import std.stdio;

void main(string[] args) { 
   char[9] greeting1 = "Hello all"; 
   writefln("%s",greeting1); 

   char[] greeting2 = "Good morning".dup; 
   writefln("%s",greeting2); 
}

When the above code is compiled and executed, it produces result something as follows −

Hello all 
Good morning

Core Language String

Strings are built-in to the core language of D. These strings are interoperable with the character array shown above. The following example shows a simple string representation.

string greeting1 = "Hello all";

Example

import std.stdio;

void main(string[] args) { 
   string greeting1 = "Hello all"; 
   writefln("%s",greeting1);  
   
   char[] greeting2 = "Good morning".dup; 
   writefln("%s",greeting2);  
   
   string greeting3 = greeting1; 
   writefln("%s",greeting3); 
}

When the above code is compiled and executed, it produces result something as follows −

Hello all 
Good morning 
Hello all 

String Concatenation

String concatenation in D programming uses the tilde(~) symbol.

Example

import std.stdio;

void main(string[] args) { 
   string greeting1 = "Good"; 
   char[] greeting2 = "morning".dup; 
   
   char[] greeting3 = greeting1~" "~greeting2; 
   writefln("%s",greeting3); 
   
   string greeting4 = "morning"; 
   string greeting5 = greeting1~" "~greeting4; 
   writefln("%s",greeting5); 
}

When the above code is compiled and executed, it produces result something as follows −

Good morning 
Good morning 

Length of String

The length of string in bytes can retrieved with the help of the length fuction.

Example

import std.stdio;  

void main(string[] args) { 
   string greeting1 = "Good"; 
   writefln("Length of string greeting1 is %d",greeting1.length); 
   
   char[] greeting2 = "morning".dup;        
   writefln("Length of string greeting2 is %d",greeting2.length); 
}

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

Length of string greeting1 is 4 
Length of string greeting2 is 7

String Comparison

String comparison is quite easy in D programming. You can use the ==, <, and > operators for string comparisons.

Example

import std.stdio; 
 
void main() { 
   string s1 = "Hello"; 
   string s2 = "World";
   string s3 = "World";
   
   if (s2 == s3) { 
      writeln("s2: ",s2," and S3: ",s3, "  are the same!"); 
   }
   
   if (s1 < s2) { 
      writeln("'", s1, "' comes before '", s2, "'."); 
   } else { 
      writeln("'", s2, "' comes before '", s1, "'."); 
   }
}

When the above code is compiled and executed, it produces result something as follows −

s2: World and S3: World are the same! 
'Hello' comes before 'World'.

Replacing Strings

We can replace strings using the string[].

Example

import std.stdio; 
import std.string; 
 
void main() {
   char[] s1 = "hello world ".dup; 
   char[] s2 = "sample".dup;
   
   s1[6..12] = s2[0..6]; 
   writeln(s1);
}

When the above code is compiled and executed, it produces result something as follows −

hello sample

Index Methods

Index methods for location of a substring in string including indexOf and lastIndexOf are explained in the following example.

Example

import std.stdio;
import std.string;

void main() { 
   char[] s1 = "hello World ".dup; 
    
   writeln("indexOf of llo in hello is ",std.string.indexOf(s1,"llo")); 
   writeln(s1); 
   writeln("lastIndexOf of O in hello is " ,std.string.lastIndexOf(s1,"O",CaseSensitive.no));
}

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

indexOf.of llo in hello is 2 
hello World  
lastIndexOf of O in hello is 7

Handling Cases

Methods used for changing cases is shown in the following example.

Example

import std.stdio;
import std.string;

void main() { 
   char[] s1 = "hello World ".dup; 
   writeln("Capitalized string of s1 is ",capitalize(s1)); 
    
   writeln("Uppercase string of s1 is ",toUpper(s1)); 
    
   writeln("Lowercase string of s1 is ",toLower(s1));   
}

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

Capitalized string of s1 is Hello world  
Uppercase string of s1 is HELLO WORLD  
Lowercase string of s1 is hello world

Restricting Characters

Restring characters in strings are shown in the following example.

Example

import std.stdio;
import std.string;

void main() { 
   string s = "H123Hello1";  
   
   string result = munch(s, "0123456789H"); 
   writeln("Restrict trailing characters:",result);  
   
   result = squeeze(s, "0123456789H"); 
   writeln("Restrict leading characters:",result); 
   
   s = "  Hello World  "; 
   writeln("Stripping leading and trailing whitespace:",strip(s)); 
}

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

Restrict trailing characters:H123H 
Restrict leading characters:ello1 
Stripping leading and trailing whitespace:Hello World
Advertisements