cmp and icmp.

bearophile bearophileHUGS at lycos.com
Thu Jul 4 16:04:49 PDT 2013


Damian:

> private void ParsePacket(in char[] data)
> {
>     static string Good = "GOOD";
>     static string Bad = "BAD";
>
>     if (data.length >= Good.length) // Prefer not to do this 
> check
>     {
>         if (std.algorithm.cmp(data[0 .. Good.length], Good) == 
> 0)
>     }
>     else if
>     {
>         // Check Bad, and so on
>     }
> }

To compare if two strings (or char arrays) are equal use ==, or 
use a switch.

For your case you can use:


import std.stdio, std.string;

void parsePacket(in char[] data) {
     if (data.startsWith("GOOD")) {
         "very good".writeln;
     } else if (data.startsWith("BAD")) {
         "very bad".writeln;
     } else {
         "something else".writeln;
         // ...
     }
}

void main() {
     parsePacket("BAD_DAY");
}


Note that idiomatic D variables and functions usually start with 
a lowercase. It's better to ask similar questions in the D.learn 
newsgroup.

Bye,
bearophile


More information about the Digitalmars-d mailing list