Create a case-insensitive startsWith

Justin Whear via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Apr 28 15:34:07 PDT 2015


On Tue, 28 Apr 2015 21:45:07 +0000, PhilipDaniels wrote:

> Beginner question. Given
> 
>    if (startsWith(input, "0x", "0X"))
> 
> How do I turn that into a case-insensitive startsWith? startsWith says
> it takes a predicate but I can't figure out how to pass it one. The
> examples all use "a == b" !? These attempts below, and other things I
> have tried, fail with "cannot deduce function from argument types".
> 
>    if (startsWith!"icmp(a, b) == 0"(input, "0x"))
>    if (startsWith!"std.uni.icmp(a, b) == 0"(input, "0x"))
>    if (startsWith!((a,b) => icmp(a,b) == 0)(input, "0x"))

The issue is that those icmp functions take strings as arguments while 
startsWith expects the predicate to take individual characters.

I believe the best solution here is to use the lazy toLowerCase function 
in std.uni and the default predicate:
  if (startsWith(input.toLowerCase, "0x".toLowerCase))



More information about the Digitalmars-d-learn mailing list