Is there a d analog of strncmp?

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Aug 21 18:45:02 PDT 2016


On Monday, August 22, 2016 00:14:31 Adam D. Ruppe via Digitalmars-d-learn 
wrote:
> int strncmp(string a, string b, int n) {
>   if(a.length > n)
>       a = a[0 .. n];
>   if(b.length > n)
>       b = b[0 .. n];
>   import std.algorithm.comparison : cmp;
>   return cmp(a, b);
> }

Aside from the imports, it can be turned into a one-liner if you use take:

return cmp(take(a, n), take(b, n));

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list