Checking if a string contains a string?

Jonathan M Davis jmdavisProg at gmx.com
Thu Oct 17 02:01:38 PDT 2013


On Thursday, October 17, 2013 08:27:17 Jeremy DeHaan wrote:
> Maybe I am just tired or something, but I tried searching for a
> way that is already included in phobos for checking if a string
> contains another string and couldn't find anything. I managed to
> write my own function for doing this, but I figured I would ask
> in case I am crazy and just missed a built in method for doing
> this.

You can you use std.algorithm.canFind if all you care about is if one string 
is in the other. You can even use std.algorithm.boyerMooreFinder with find to 
use the boyer moore algorithm (though in that case, since for some reason, 
canFind doesn't have an overload which takes a BoyerMooreFinder, you'd have to 
use find and check whether the result was empty - and if it is, then the string 
wasn't there).

But depending on what exactly you're trying to do, many of the functions in 
the "searching" section at the top of std.algorithm could be used:

http://dlang.org/phobos/std_algorithm.html

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list