www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Checking if a string contains a string?

reply "Jeremy DeHaan" <dehaan.jeremiah gmail.com> writes:
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.
Oct 16 2013
next sibling parent "evilrat" <evilrat666 gmail.com> writes:
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 ?
Oct 16 2013
prev sibling next sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
Oct 16 2013
prev sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
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
Oct 17 2013