Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

String Comparison Operators

Part of TutorialFundamentals

Description

Shows how strings are compared.

Example

import std.stdio;

int main() {
  string str1 = "Aardvark";
  string str2 = "Zebra";
  string str3 = "1";


  /* "less than" operator */
  if ( str1 < str2 ) 
    writefln("Aardvark comes before Zebra.");
  else
    writefln("Aardvark comes after Zebra.");


  /* "greater" than operator */
  if ( str1 > str2 ) 
    writefln("Aardvark comes after Zebra.");
  else
    writefln("Aardvark comes before Zebra.");


  /* "equality" operator */
  if ( str1 == str2 ) 
    writefln("Aardvark is Zebra?!");
  else
    writefln("Aardvark is NOT Zebra.");


  /* "concatenation" operator */
  for (int i = 0; i < 3; i++) 
    str3 ~= "0";
  writefln("%s", str3);

  return 0;
}

Source

Based on string_ops.html by jcc7.