Thread overview
Checking if a string contains a string?
Oct 17, 2013
Jeremy DeHaan
Oct 17, 2013
evilrat
Oct 17, 2013
Ali Çehreli
Oct 17, 2013
Jonathan M Davis
October 17, 2013
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.
October 17, 2013
On Thursday, 17 October 2013 at 06:27:19 UTC, 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.

have you tried std.string.indexOf ?
October 17, 2013
On 10/16/2013 11:27 PM, 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.

There are several search functions in std.algorithm.

findSplit() is a versatile one, providing three ranges: before, at, and after the searched string.

import std.algorithm;

void main()
{
    auto s = "hello cool world";
    auto result = s.findSplit("cool");

    assert(result[0] == "hello ");
    assert(result[1] == "cool");
    assert(result[2] == " world");
}

Ali

October 17, 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